PresentationPresenter.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // Created by Tom Baranes on 16/07/16.
  3. // Copyright © 2016 Jake Lin. All rights reserved.
  4. //
  5. import UIKit
  6. public class PresentationPresenter: NSObject {
  7. private var presentationAnimationType: PresentationAnimationType
  8. public var dismissalAnimationType: PresentationAnimationType?
  9. public var presentationConfiguration: PresentationConfiguration?
  10. public var transitionDuration: Duration {
  11. didSet {
  12. if oldValue != transitionDuration {
  13. updateTransitionDuration()
  14. }
  15. }
  16. }
  17. // animation controller
  18. fileprivate var animator: AnimatedPresenting?
  19. public init(presentationAnimationType: PresentationAnimationType, transitionDuration: Duration = defaultPresentationDuration) {
  20. self.presentationAnimationType = presentationAnimationType
  21. self.transitionDuration = transitionDuration
  22. super.init()
  23. updateTransitionDuration()
  24. if presentationAnimationType.systemTransition == nil {
  25. animator = AnimatorFactory.makeAnimator(presentationAnimationType: presentationAnimationType, transitionDuration: transitionDuration)
  26. }
  27. }
  28. // MARK: - Private
  29. private func updateTransitionDuration() {
  30. if transitionDuration.isNaN {
  31. transitionDuration = defaultPresentationDuration
  32. }
  33. }
  34. }
  35. extension PresentationPresenter: UIViewControllerTransitioningDelegate {
  36. // MARK: - presentation
  37. public func presentationController(forPresented presented: UIViewController,
  38. presenting: UIViewController?,
  39. source: UIViewController) -> UIPresentationController? {
  40. guard let presentationConfiguration = presentationConfiguration else {
  41. return nil
  42. }
  43. return AnimatablePresentationController(presentedViewController: presented,
  44. presentingViewController: presenting,
  45. presentationConfiguration: presentationConfiguration)
  46. }
  47. // MARK: - animation controller
  48. public func animationController(forPresented presented: UIViewController,
  49. presenting: UIViewController,
  50. source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  51. animator?.transitionDuration = transitionDuration
  52. return animator
  53. }
  54. public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  55. guard let dismissalAnimationType = dismissalAnimationType else {
  56. return animator
  57. }
  58. if dismissalAnimationType.systemTransition != nil {
  59. return nil
  60. }
  61. return AnimatorFactory.makeAnimator(presentationAnimationType: dismissalAnimationType, transitionDuration: transitionDuration)
  62. }
  63. }