PopupViewController.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // PopupViewController.swift
  3. // TUIRoomKit
  4. //
  5. // Created by aby on 2024/6/26.
  6. //
  7. import UIKit
  8. import Factory
  9. class PopupViewController: UIViewController {
  10. let contentView: UIView
  11. private let visualEffectView: UIView = {
  12. let blurEffect = UIBlurEffect(style: .dark)
  13. let view = UIVisualEffectView(effect: blurEffect)
  14. view.frame = UIScreen.main.bounds
  15. view.alpha = 0
  16. return view
  17. }()
  18. public init(contentView: UIView) {
  19. self.contentView = contentView
  20. super.init(nibName: nil, bundle: nil)
  21. modalPresentationStyle = .custom
  22. transitioningDelegate = self
  23. }
  24. required init?(coder: NSCoder) {
  25. fatalError("init(coder:) has not been implemented")
  26. }
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. constructViewHierarchy()
  30. activateConstraints()
  31. }
  32. public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  33. return .portrait
  34. }
  35. func constructViewHierarchy() {
  36. view.addSubview(contentView)
  37. }
  38. func activateConstraints() {
  39. contentView.snp.remakeConstraints { make in
  40. make.leading.trailing.bottom.equalToSuperview()
  41. }
  42. }
  43. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  44. super.touchesBegan(touches, with: event)
  45. guard let touch = touches.first else { return }
  46. let point = touch.location(in: contentView)
  47. guard !contentView.layer.contains(point) else { return }
  48. route.dismiss(animated: true)
  49. }
  50. // MARK: - private property.
  51. @Injected(\.navigation) private var route: Route
  52. }
  53. extension PopupViewController: UIViewControllerTransitioningDelegate {
  54. public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) ->
  55. UIViewControllerAnimatedTransitioning? {
  56. let transitionAnimator = AlertTransitionAnimator()
  57. transitionAnimator.alertTransitionStyle = .present
  58. if interfaceOrientation.isPortrait {
  59. transitionAnimator.alertTransitionPosition = .bottom
  60. } else {
  61. transitionAnimator.alertTransitionPosition = .right
  62. }
  63. source.view.addSubview(visualEffectView)
  64. UIView.animate(withDuration: transitionAnimator.duration) { [weak self] in
  65. guard let self = self else { return }
  66. self.visualEffectView.alpha = 1
  67. }
  68. return transitionAnimator
  69. }
  70. public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  71. let transitionAnimator = AlertTransitionAnimator()
  72. transitionAnimator.alertTransitionStyle = .dismiss
  73. if interfaceOrientation.isPortrait {
  74. transitionAnimator.alertTransitionPosition = .bottom
  75. } else {
  76. transitionAnimator.alertTransitionPosition = .right
  77. }
  78. UIView.animate(withDuration: transitionAnimator.duration) { [weak self] in
  79. guard let self = self else { return }
  80. self.visualEffectView.alpha = 0
  81. } completion: { [weak self] finished in
  82. guard let self = self else { return }
  83. if finished {
  84. self.visualEffectView.removeFromSuperview()
  85. }
  86. }
  87. return transitionAnimator
  88. }
  89. }