PopUpViewController.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // PopUpViewController.swift
  3. // TUIRoomKit
  4. //
  5. // Created by janejntang on 2023/1/12.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. protocol PopUpViewModelFactory {
  11. func makeRootViewModel(viewType: PopUpViewType, height:CGFloat, backgroundColor: UIColor) -> PopUpViewModel
  12. }
  13. class PopUpViewController: UIViewController {
  14. let viewModel: PopUpViewModel
  15. var duration = 0.5
  16. var alertTransitionStyle: AlertTransitionAnimator.AlertTransitionStyle = .present
  17. var alertTransitionPosition: AlertTransitionAnimator.AlertTransitionPosition = .bottom
  18. var transitionAnimator: AlertTransitionAnimator?
  19. override var shouldAutorotate: Bool {
  20. return true
  21. }
  22. override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  23. return .allButUpsideDown
  24. }
  25. init(popUpViewModelFactory: PopUpViewModelFactory, viewType: PopUpViewType, height: CGFloat, backgroundColor: UIColor) {
  26. viewModel = popUpViewModelFactory.makeRootViewModel(viewType: viewType, height: height, backgroundColor: backgroundColor)
  27. super.init(nibName: nil, bundle: nil)
  28. modalPresentationStyle = .custom
  29. transitioningDelegate = self
  30. if isLandscape {
  31. self.alertTransitionPosition = .right
  32. } else {
  33. self.alertTransitionPosition = .bottom
  34. }
  35. }
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. override func loadView() {
  40. let rootView = PopUpView(viewModel: viewModel)
  41. rootView.responder = self
  42. view = rootView
  43. }
  44. override func viewWillAppear(_ animated: Bool) {
  45. super.viewWillAppear(animated)
  46. navigationController?.setNavigationBarHidden(true, animated: false)
  47. UIApplication.shared.isIdleTimerDisabled = true
  48. }
  49. deinit {
  50. debugPrint("deinit \(self)")
  51. }
  52. }
  53. extension PopUpViewController: UIViewControllerTransitioningDelegate {
  54. func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) ->
  55. UIViewControllerAnimatedTransitioning? {
  56. transitionAnimator = AlertTransitionAnimator()
  57. transitionAnimator?.alertTransitionStyle = .present
  58. transitionAnimator?.alertTransitionPosition = alertTransitionPosition
  59. transitionAnimator?.duration = duration
  60. return transitionAnimator
  61. }
  62. func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  63. transitionAnimator?.alertTransitionStyle = .dismiss
  64. return transitionAnimator
  65. }
  66. }
  67. extension PopUpViewController: PopUpViewResponder {
  68. func updateAlertTransitionPosition(position: AlertTransitionAnimator.AlertTransitionPosition) {
  69. transitionAnimator?.alertTransitionPosition = position
  70. }
  71. }