Created
December 10, 2016 22:18
-
-
Save wh1pch81n/f0696370f76bfa55caf3ad88b33a4f0c to your computer and use it in GitHub Desktop.
A wrapper that allows you to set animation and completion handler in a more consistent way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIKit.UIView { | |
public class Animator { | |
fileprivate var animatorBlock: ((@escaping () -> (), @escaping (Bool) -> ()) -> ()) | |
fileprivate var animations: () -> () = { _ in } | |
public init(animatorBlock: @escaping ((@escaping () -> (), @escaping (Bool) -> ()) -> ())) { | |
self.animatorBlock = animatorBlock | |
} | |
public func animations(_ animations: @escaping () -> ()) -> Self { | |
self.animations = animations | |
return self | |
} | |
public func completion(_ completion: @escaping (Bool) -> ()) { | |
animatorBlock(animations, completion) | |
} | |
} | |
public static func animate(withDuration duration: TimeInterval) -> Animator { | |
return Animator(animatorBlock: { | |
UIView.animate(withDuration: duration, animations: $0, completion: $1) | |
}) | |
} | |
public static func animate(withDuration duration: TimeInterval, delay: TimeInterval, options: UIViewAnimationOptions) -> Animator { | |
return Animator(animatorBlock: { | |
UIView.animate(withDuration: duration, delay: delay, options: options, animations: $0, completion: $1) | |
}) | |
} | |
public static func animate(withDuration duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions ) -> Animator { | |
return Animator(animatorBlock: { | |
UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: $0, completion: $1) | |
}) | |
} | |
public static func animateKeyframes(withDuration duration: TimeInterval, delay: TimeInterval, options: UIViewKeyframeAnimationOptions) -> Animator { | |
return Animator(animatorBlock: { | |
UIView.animateKeyframes(withDuration: duration, delay: delay, options: options, animations: $0, completion: $1) | |
}) | |
} | |
} | |
// Example usage | |
// Vanilla animation | |
UIView.animate(withDuration: 5) | |
.animations { | |
self.view.backgroundColor = .red | |
} | |
.completion { _ in } | |
// Animation with delay | |
UIView.animate(withDuration: 5, delay: 1, options: UIViewAnimationOptions.curveEaseInOut) | |
.animations { | |
self.view.backgroundColor = .green | |
}.completion { _ in } | |
// Animation with spring velocity | |
let v = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) | |
v.backgroundColor = .purple | |
self.view.addSubview(v) | |
UIView.animate(withDuration: 5, delay: 1, usingSpringWithDamping: 0.1, initialSpringVelocity: 40, options: UIViewAnimationOptions.curveEaseInOut) | |
.animations { | |
v.frame.origin.x = 150 | |
}.completion({ _ in | |
v.frame.origin.x = 0 | |
}) | |
// Animation with key frames. | |
UIView.animateKeyframes(withDuration: 5 | |
, delay: 0 | |
, options: UIViewKeyframeAnimationOptions.calculationModeLinear) | |
.animations { | |
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.2) { | |
v.frame.origin = CGPoint(x: 20, y: 0) | |
} | |
UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.2) { | |
v.frame.origin = CGPoint(x: 20, y: 20) | |
} | |
} | |
.completion {_ in} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment