PanInteractiveAnimator.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Created by Jake Lin on 3/3/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. // Pan interactive animator: pan gesture transition controller
  7. public class PanInteractiveAnimator: InteractiveAnimator {
  8. override func makeGestureRecognizer() -> UIGestureRecognizer {
  9. return UIPanGestureRecognizer(target: self, action: #selector(handleGesture(for:)))
  10. }
  11. override func calculateProgress(for gestureRecognizer: UIGestureRecognizer) -> (progress: CGFloat, shouldFinishInteractiveTransition: Bool) {
  12. guard let gestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer,
  13. let superview = gestureRecognizer.view?.superview else {
  14. return (0, false)
  15. }
  16. let translation = gestureRecognizer.translation(in: superview)
  17. let velocity = gestureRecognizer.velocity(in: superview)
  18. var progress: CGFloat
  19. let distance: CGFloat
  20. let speed: CGFloat
  21. switch interactiveGestureType {
  22. case let .pan(direction):
  23. switch direction {
  24. case .horizontal:
  25. distance = superview.frame.width
  26. progress = abs(translation.x / distance)
  27. speed = abs(velocity.x)
  28. case .left:
  29. distance = superview.frame.width
  30. progress = translation.x / distance
  31. speed = velocity.x
  32. case .right:
  33. distance = superview.frame.width
  34. progress = -(translation.x / distance)
  35. speed = -velocity.x
  36. case .vertical:
  37. distance = superview.frame.height
  38. progress = abs(translation.y / distance)
  39. speed = abs(velocity.y)
  40. case .top:
  41. distance = superview.frame.height
  42. progress = translation.y / distance
  43. speed = velocity.y
  44. case .bottom:
  45. distance = superview.frame.height
  46. progress = -translation.y / distance
  47. speed = -velocity.y
  48. default:
  49. return (0, false)
  50. }
  51. default:
  52. return (0, false)
  53. }
  54. progress = min(max(progress, 0), 0.99)
  55. // Finish the transition when pass the threathold
  56. let shouldFinishInteractiveTransition = progress > 0.5 || speed > 1000
  57. return (progress, shouldFinishInteractiveTransition)
  58. }
  59. }