Navigator.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Created by Jake Lin on 2/24/16.
  3. // Copyright (c) 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. /**
  7. Navigator for `UINavigationController` or `UITabBarController` to support custom transition animation
  8. */
  9. public class Navigator: NSObject {
  10. var transitionAnimationType: TransitionAnimationType
  11. var transitionDuration: Duration = defaultTransitionDuration
  12. // animation controller
  13. fileprivate var animator: AnimatedTransitioning?
  14. // interaction controller
  15. fileprivate var interactiveAnimator: InteractiveAnimator?
  16. public init(transitionAnimationType: TransitionAnimationType,
  17. transitionDuration: Duration = defaultTransitionDuration,
  18. interactiveGestureType: InteractiveGestureType? = nil) {
  19. self.transitionAnimationType = transitionAnimationType
  20. self.transitionDuration = transitionDuration
  21. super.init()
  22. animator = AnimatorFactory.makeAnimator(transitionAnimationType: transitionAnimationType, transitionDuration: transitionDuration)
  23. // If interactiveGestureType has been set
  24. if let interactiveGestureType = interactiveGestureType {
  25. // If configured as `.Default` then use the default interactive gesture type from the Animator
  26. switch interactiveGestureType {
  27. case .default:
  28. if let interactiveGestureType = animator?.interactiveGestureType {
  29. interactiveAnimator = InteractiveAnimatorFactory.makeInteractiveAnimator(interactiveGestureType: interactiveGestureType,
  30. transitionType: .navigationTransition(.pop))
  31. }
  32. default:
  33. interactiveAnimator = InteractiveAnimatorFactory.makeInteractiveAnimator(interactiveGestureType: interactiveGestureType,
  34. transitionType: .navigationTransition(.pop))
  35. }
  36. }
  37. }
  38. }
  39. // MARK: - navigation controller delegate
  40. extension Navigator: UINavigationControllerDelegate {
  41. // MARK: - animation controller
  42. public func navigationController(_ navigationController: UINavigationController,
  43. animationControllerFor operation: UINavigationController.Operation,
  44. from fromVC: UIViewController,
  45. to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  46. interactiveAnimator?.connectGestureRecognizer(to: toVC)
  47. if operation == .push {
  48. animator?.transitionDuration = transitionDuration
  49. return animator
  50. } else if operation == .pop {
  51. // Use the reverse animation
  52. if let reverseTransitionAnimationType = animator?.reverseAnimationType {
  53. return AnimatorFactory.makeAnimator(transitionAnimationType: reverseTransitionAnimationType, transitionDuration: transitionDuration)
  54. }
  55. }
  56. return nil
  57. }
  58. // MARK: - interaction controller
  59. public func navigationController(_ navigationController: UINavigationController,
  60. interactionControllerFor animationController: UIViewControllerAnimatedTransitioning)
  61. -> UIViewControllerInteractiveTransitioning? {
  62. if let interactiveAnimator = interactiveAnimator, interactiveAnimator.interacting {
  63. return interactiveAnimator
  64. }
  65. return nil
  66. }
  67. }
  68. // MARK: - tabbar controller delegate
  69. extension Navigator: UITabBarControllerDelegate {
  70. // MARK: - animation controller
  71. public func tabBarController(_ tabBarController: UITabBarController,
  72. animationControllerForTransitionFrom fromVC: UIViewController,
  73. to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  74. interactiveAnimator?.connectGestureRecognizer(to: toVC)
  75. #if swift(>=4.2)
  76. guard let viewControllers = tabBarController.viewControllers,
  77. let fromVCIndex = viewControllers.firstIndex(of: fromVC),
  78. let toVCIndex = viewControllers.firstIndex(of: toVC) else {
  79. return nil
  80. }
  81. #else
  82. guard let viewControllers = tabBarController.viewControllers,
  83. let fromVCIndex = viewControllers.index(of: fromVC),
  84. let toVCIndex = viewControllers.index(of: toVC) else {
  85. return nil
  86. }
  87. #endif
  88. if toVCIndex > fromVCIndex, let reverseTransitionAnimationType = animator?.reverseAnimationType {
  89. return AnimatorFactory.makeAnimator(transitionAnimationType: reverseTransitionAnimationType, transitionDuration: transitionDuration)
  90. }
  91. animator?.transitionDuration = transitionDuration
  92. return animator
  93. }
  94. // MARK: - interaction controller
  95. public func tabBarController(_ tabBarController: UITabBarController,
  96. interactionControllerFor animationController: UIViewControllerAnimatedTransitioning)
  97. -> UIViewControllerInteractiveTransitioning? {
  98. if let interactiveAnimator = interactiveAnimator, interactiveAnimator.interacting {
  99. return interactiveAnimator
  100. }
  101. return nil
  102. }
  103. }