AnimatableNavigationController.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // Created by Jake Lin on 2/20/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. open class AnimatableNavigationController: UINavigationController, TransitionAnimatable {
  7. // MARK: - TransitionAnimatable
  8. @IBInspectable var _transitionAnimationType: String? {
  9. didSet {
  10. if let _transitionAnimationType = _transitionAnimationType {
  11. transitionAnimationType = TransitionAnimationType(string: _transitionAnimationType)
  12. }
  13. }
  14. }
  15. open var transitionAnimationType: TransitionAnimationType = .none {
  16. didSet {
  17. configureNavigationControllerDelegate()
  18. }
  19. }
  20. @IBInspectable open var transitionDuration: Double = .nan {
  21. didSet {
  22. configureNavigationControllerDelegate()
  23. }
  24. }
  25. open var interactiveGestureType: InteractiveGestureType = .none {
  26. didSet {
  27. configureNavigationControllerDelegate()
  28. }
  29. }
  30. @IBInspectable var _interactiveGestureType: String? {
  31. didSet {
  32. if let _interactiveGestureType = _interactiveGestureType {
  33. interactiveGestureType = InteractiveGestureType(string: _interactiveGestureType)
  34. }
  35. }
  36. }
  37. // MARK: - Private
  38. // Must have a property to keep the reference alive because `UINavigationController.delegate` is `weak`
  39. fileprivate var navigator: Navigator?
  40. fileprivate func configureNavigationControllerDelegate() {
  41. if case .none = transitionAnimationType {
  42. navigator = nil
  43. delegate = nil
  44. return
  45. }
  46. var duration = transitionDuration
  47. // Set the default duration for transition
  48. if transitionDuration.isNaN {
  49. duration = defaultTransitionDuration
  50. }
  51. if case .none = interactiveGestureType {
  52. navigator = Navigator(transitionAnimationType: transitionAnimationType, transitionDuration: duration)
  53. } else {
  54. navigator = Navigator(transitionAnimationType: transitionAnimationType,
  55. transitionDuration: duration,
  56. interactiveGestureType: interactiveGestureType)
  57. }
  58. delegate = navigator
  59. }
  60. }