AlertTransitionAnimator.swift 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // AlertTransitionAnimator.swift
  3. // TUIRoomKit
  4. //
  5. // Created by janejntang on 2023/4/6.
  6. //
  7. import Foundation
  8. class AlertTransitionAnimator : NSObject {
  9. enum AlertTransitionStyle {
  10. case present
  11. case dismiss
  12. }
  13. enum AlertTransitionPosition {
  14. case bottom
  15. case right
  16. }
  17. var duration = 0.5
  18. var alertTransitionStyle: AlertTransitionStyle = .present
  19. var alertTransitionPosition: AlertTransitionPosition = .bottom
  20. deinit {
  21. debugPrint("deinit \(self)")
  22. }
  23. }
  24. extension AlertTransitionAnimator {
  25. private func presentTransition(transitionContext: UIViewControllerContextTransitioning) {
  26. guard let fromController = transitionContext.viewController(forKey: .from), let toController = transitionContext.viewController(forKey: .to)
  27. else { return }
  28. guard let fromView = fromController.view, let toView = toController.view else { return }
  29. let contentView = transitionContext.containerView
  30. fromView.tintAdjustmentMode = .normal
  31. fromView.isUserInteractionEnabled = false
  32. toView.isUserInteractionEnabled = false
  33. contentView.addSubview(toView)
  34. switch alertTransitionPosition {
  35. case .bottom:
  36. toView.frame = CGRect(x: 0, y: contentView.bounds.size.height, width: contentView.bounds.size.width, height:
  37. contentView.bounds.size.height)
  38. case .right:
  39. toView.frame = CGRect(x: contentView.bounds.size.width, y: 0, width: contentView.bounds.size.width/2, height:
  40. contentView.bounds.size.height)
  41. }
  42. UIView.animate(withDuration: duration, animations: { [weak self] in
  43. guard let self = self else { return }
  44. switch self.alertTransitionPosition {
  45. case .bottom:
  46. toView.frame = CGRect(x: 0, y: 0, width: contentView.bounds.size.width, height: contentView.bounds.size.height)
  47. case .right:
  48. toView.frame = CGRect(x: 0, y: 0, width: contentView.bounds.size.width, height: contentView.bounds.size.height)
  49. }
  50. }) { (finish) in
  51. fromView.isUserInteractionEnabled = true
  52. toView.isUserInteractionEnabled = true
  53. transitionContext.completeTransition(true)
  54. }
  55. }
  56. private func dismissTransition(transitionContext: UIViewControllerContextTransitioning) {
  57. guard let fromController = transitionContext.viewController(forKey: .from), let toController = transitionContext.viewController(forKey: .to)
  58. else { return }
  59. guard let fromView = fromController.view, let toView = toController.view else { return }
  60. fromView.isUserInteractionEnabled = false
  61. toView.isUserInteractionEnabled = false
  62. let contentView = transitionContext.containerView
  63. UIView.animate(withDuration: duration, animations: { [weak self] in
  64. guard let self = self else { return }
  65. switch self.alertTransitionPosition {
  66. case .bottom:
  67. fromView.frame = CGRect(x: 0, y: contentView.bounds.size.height, width: contentView.bounds.size.width, height:
  68. contentView.bounds.size.height)
  69. case .right:
  70. fromView.frame = CGRect(x: contentView.bounds.size.width, y: 0, width: contentView.bounds.size.width, height:
  71. contentView.bounds.size.height)
  72. }
  73. }) { (finish) in
  74. fromView.removeFromSuperview()
  75. toView.isUserInteractionEnabled = true
  76. transitionContext.completeTransition(true)
  77. }
  78. }
  79. }
  80. extension AlertTransitionAnimator: UIViewControllerAnimatedTransitioning {
  81. func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
  82. return duration
  83. }
  84. func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
  85. switch alertTransitionStyle {
  86. case .present:
  87. presentTransition(transitionContext: transitionContext)
  88. case .dismiss:
  89. dismissTransition(transitionContext: transitionContext)
  90. }
  91. }
  92. }