TransitionPresenter.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Created by Jake Lin on 2/24/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. /**
  7. TransitionPresenter for `UIViewController` to support custom transition animation for Present and Dismiss
  8. */
  9. public class TransitionPresenter: NSObject {
  10. fileprivate var transitionAnimationType: TransitionAnimationType
  11. var transitionDuration: Duration {
  12. didSet {
  13. if oldValue != transitionDuration {
  14. updateTransitionDuration()
  15. }
  16. }
  17. }
  18. var interactiveGestureType: InteractiveGestureType? {
  19. // Update `interactiveAnimator` if needed
  20. didSet {
  21. if oldValue?.stringValue != interactiveGestureType?.stringValue {
  22. updateInteractiveAnimator()
  23. }
  24. }
  25. }
  26. // animation controller
  27. fileprivate var animator: AnimatedTransitioning?
  28. // interaction controller
  29. fileprivate var interactiveAnimator: InteractiveAnimator?
  30. public init(transitionAnimationType: TransitionAnimationType,
  31. transitionDuration: Duration = defaultTransitionDuration,
  32. interactiveGestureType: InteractiveGestureType? = nil) {
  33. self.transitionAnimationType = transitionAnimationType
  34. self.transitionDuration = transitionDuration
  35. super.init()
  36. updateTransitionDuration()
  37. animator = AnimatorFactory.makeAnimator(transitionAnimationType: transitionAnimationType, transitionDuration: transitionDuration)
  38. self.interactiveGestureType = interactiveGestureType
  39. updateInteractiveAnimator()
  40. }
  41. // MARK: - Private
  42. fileprivate func updateTransitionDuration() {
  43. if transitionDuration.isNaN {
  44. transitionDuration = defaultTransitionDuration
  45. }
  46. }
  47. fileprivate func updateInteractiveAnimator() {
  48. // If interactiveGestureType has been set
  49. if let interactiveGestureType = interactiveGestureType {
  50. // If configured as `.Default` then use the default interactive gesture type from the Animator
  51. switch interactiveGestureType {
  52. case .default:
  53. if let interactiveGestureType = animator?.interactiveGestureType {
  54. interactiveAnimator = InteractiveAnimatorFactory.makeInteractiveAnimator(interactiveGestureType: interactiveGestureType,
  55. transitionType: .presentationTransition(.dismissal))
  56. }
  57. default:
  58. interactiveAnimator = InteractiveAnimatorFactory.makeInteractiveAnimator(interactiveGestureType: interactiveGestureType,
  59. transitionType: .presentationTransition(.dismissal))
  60. }
  61. } else {
  62. interactiveAnimator = nil
  63. }
  64. }
  65. }
  66. extension TransitionPresenter: UIViewControllerTransitioningDelegate {
  67. // MARK: - animation controller
  68. public func animationController(forPresented presented: UIViewController,
  69. presenting: UIViewController,
  70. source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  71. animator?.transitionDuration = transitionDuration
  72. interactiveAnimator?.connectGestureRecognizer(to: presented)
  73. return animator
  74. }
  75. public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  76. // Use the reverse animation
  77. if let reverseTransitionAnimationType = animator?.reverseAnimationType {
  78. return AnimatorFactory.makeAnimator(transitionAnimationType: reverseTransitionAnimationType, transitionDuration: transitionDuration)
  79. }
  80. return nil
  81. }
  82. // MARK: - interaction controller
  83. public func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
  84. if let interactiveAnimator = interactiveAnimator, interactiveAnimator.interacting {
  85. return interactiveAnimator
  86. } else {
  87. return nil
  88. }
  89. }
  90. }