Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement