Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. extension UIKit.UIView {
  2.  
  3. public class Animator {
  4. fileprivate var animatorBlock: ((@escaping () -> (), @escaping (Bool) -> ()) -> ())
  5. fileprivate var animations: () -> () = { _ in }
  6.  
  7. public init(animatorBlock: @escaping ((@escaping () -> (), @escaping (Bool) -> ()) -> ())) {
  8. self.animatorBlock = animatorBlock
  9. }
  10.  
  11. public func animations(_ animations: @escaping () -> ()) -> Self {
  12. self.animations = animations
  13. return self
  14. }
  15.  
  16. public func completion(_ completion: @escaping (Bool) -> ()) {
  17. animatorBlock(animations, completion)
  18. }
  19. }
  20.  
  21. public static func animate(withDuration duration: TimeInterval) -> Animator {
  22. return Animator(animatorBlock: {
  23. UIView.animate(withDuration: duration, animations: $0, completion: $1)
  24. })
  25. }
  26.  
  27. public static func animate(withDuration duration: TimeInterval, delay: TimeInterval, options: UIViewAnimationOptions) -> Animator {
  28. return Animator(animatorBlock: {
  29. UIView.animate(withDuration: duration, delay: delay, options: options, animations: $0, completion: $1)
  30. })
  31. }
  32.  
  33. public static func animate(withDuration duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions ) -> Animator {
  34. return Animator(animatorBlock: {
  35. UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: $0, completion: $1)
  36. })
  37. }
  38.  
  39. public static func animateKeyframes(withDuration duration: TimeInterval, delay: TimeInterval, options: UIViewKeyframeAnimationOptions) -> Animator {
  40. return Animator(animatorBlock: {
  41. UIView.animateKeyframes(withDuration: duration, delay: delay, options: options, animations: $0, completion: $1)
  42. })
  43. }
  44.  
  45. }
  46.  
  47.  
  48. // Example usage
  49.  
  50. // Vanilla animation
  51. UIView.animate(withDuration: 5)
  52. .animations {
  53. self.view.backgroundColor = .red
  54. }
  55. .completion { _ in }
  56.  
  57. // Animation with delay
  58. UIView.animate(withDuration: 5, delay: 1, options: UIViewAnimationOptions.curveEaseInOut)
  59. .animations {
  60. self.view.backgroundColor = .green
  61. }.completion { _ in }
  62.  
  63. // Animation with spring velocity
  64. let v = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
  65. v.backgroundColor = .purple
  66. self.view.addSubview(v)
  67. UIView.animate(withDuration: 5, delay: 1, usingSpringWithDamping: 0.1, initialSpringVelocity: 40, options: UIViewAnimationOptions.curveEaseInOut)
  68. .animations {
  69. v.frame.origin.x = 150
  70. }.completion({ _ in
  71. v.frame.origin.x = 0
  72. })
  73.  
  74.  
  75. // Animation with key frames.
  76. UIView.animateKeyframes(withDuration: 5
  77. , delay: 0
  78. , options: UIViewKeyframeAnimationOptions.calculationModeLinear)
  79. .animations {
  80. UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.2) {
  81. v.frame.origin = CGPoint(x: 20, y: 0)
  82. }
  83. UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.2) {
  84. v.frame.origin = CGPoint(x: 20, y: 20)
  85. }
  86. }
  87. .completion {_ in}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement