ZoomAnimator.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 ZoomAnimator: NSObject, AnimatedPresenting {
  7. // MARK: - AnimatedPresenting
  8. public var transitionDuration: Duration = defaultTransitionDuration
  9. // MARK: - Life cycle
  10. public init(duration: Duration) {
  11. transitionDuration = duration
  12. super.init()
  13. }
  14. }
  15. // MARK: - Animator
  16. extension ZoomAnimator: UIViewControllerAnimatedTransitioning {
  17. public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
  18. return retrieveTransitionDuration(transitionContext: transitionContext)
  19. }
  20. public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
  21. let (fromView, toView, tempContainerView) = retrieveViews(transitionContext: transitionContext)
  22. let isPresenting = self.isPresenting(transitionContext: transitionContext)
  23. guard let containerView = tempContainerView, let animatingView = isPresenting ? toView : fromView else {
  24. transitionContext.completeTransition(true)
  25. return
  26. }
  27. if isPresenting {
  28. containerView.addSubview(animatingView)
  29. }
  30. animateZoom(animatingView: animatingView, isPresenting: isPresenting) {
  31. if !isPresenting {
  32. fromView?.removeFromSuperview()
  33. }
  34. transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
  35. }
  36. }
  37. }
  38. // MARK: - Animation
  39. private extension ZoomAnimator {
  40. func animateZoom(animatingView: UIView, isPresenting: Bool, completion: @escaping AnimatableCompletion) {
  41. if isPresenting {
  42. animatePresengingZoom(animatingView: animatingView, completion: completion)
  43. } else {
  44. animateDismissingZoom(animatingView: animatingView, completion: completion)
  45. }
  46. }
  47. func animatePresengingZoom(animatingView: UIView, completion: @escaping AnimatableCompletion) {
  48. animatingView.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
  49. UIView.animate(withDuration: transitionDuration,
  50. delay: 0.0,
  51. usingSpringWithDamping: 0.6,
  52. initialSpringVelocity: 0,
  53. options: [.curveEaseOut],
  54. animations: {
  55. animatingView.transform = CGAffineTransform(scaleX: 1, y: 1)
  56. }) { _ in
  57. completion()
  58. }
  59. }
  60. func animateDismissingZoom(animatingView: UIView, completion: @escaping AnimatableCompletion) {
  61. UIView.animate(withDuration: transitionDuration,
  62. delay: 0.0,
  63. usingSpringWithDamping: 0.6,
  64. initialSpringVelocity: 0,
  65. options: [.curveEaseIn],
  66. animations: {
  67. animatingView.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
  68. animatingView.alpha = 0.0
  69. }) { _ in
  70. completion()
  71. }
  72. }
  73. }