AnimatedTransitioning.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // Created by Jake Lin on 2/24/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. /**
  7. AnimatedTransitioning is the protocol of all Animator subclasses
  8. */
  9. public protocol AnimatedTransitioning: ViewControllerAnimatedTransitioning {
  10. /**
  11. Transition animation type: used to specify the transition animation.
  12. */
  13. var transitionAnimationType: TransitionAnimationType { get set }
  14. /**
  15. Reverse animation type: used to specify the revers animation for pop or dismiss.
  16. */
  17. var reverseAnimationType: TransitionAnimationType? { get set }
  18. /**
  19. Interactive gesture type: used to specify the gesture type to pop or dismiss.
  20. */
  21. var interactiveGestureType: InteractiveGestureType? { get set }
  22. }
  23. public extension AnimatedTransitioning {
  24. func animateWithCATransition(transitionContext: UIViewControllerContextTransitioning,
  25. type: TransitionAnimationType.SystemTransitionType,
  26. subtype: CATransitionSubtype?) {
  27. let (_, tempToView, tempContainerView) = retrieveViews(transitionContext: transitionContext)
  28. guard let toView = tempToView, let containerView = tempContainerView else {
  29. transitionContext.completeTransition(true)
  30. return
  31. }
  32. let (_, tempToViewController, _) = retrieveViewControllers(transitionContext: transitionContext)
  33. if let toViewController = tempToViewController {
  34. toView.frame = transitionContext.finalFrame(for: toViewController)
  35. }
  36. containerView.addSubview(toView)
  37. CALayer.animate({
  38. let transition = CATransition()
  39. #if swift(>=4.2)
  40. transition.type = CATransitionType(rawValue: type.rawValue)
  41. #else
  42. transition.type = type.rawValue
  43. #endif
  44. if let subtype = subtype {
  45. transition.subtype = subtype
  46. }
  47. transition.duration = self.transitionDuration(using: transitionContext)
  48. transition.timingFunctionType = .easeOutCubic
  49. containerView.layer.add(transition, forKey: kCATransition)
  50. },
  51. completion: {
  52. transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
  53. })
  54. }
  55. }