Created
September 29, 2022 07:52
-
-
Save yucelokan/dddfe68b4ab6cdea605dd10a90ff0ab0 to your computer and use it in GitHub Desktop.
Animation extension for UIView.
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
public extension UIView { | |
func fader( | |
duration: CGFloat, | |
delay: CGFloat = 0, | |
fromValue: CGFloat = 0, | |
toValue: CGFloat = 1, | |
curve: UIView.AnimationCurve = .easeOut, | |
completion: (() -> Void)? = nil | |
) { | |
alpha = fromValue | |
let animator = UIViewPropertyAnimator(duration: duration, curve: curve) { [weak self] in | |
self?.alpha = toValue | |
} | |
animator.addCompletion { position in | |
switch position { | |
case .end: | |
completion?() | |
default: | |
break | |
} | |
} | |
animator.startAnimation(afterDelay: delay) | |
} | |
func slider( | |
duration: CGFloat, | |
fromValue: CGPoint, | |
toValue: CGPoint, | |
delay: CGFloat = 0, | |
curve: UIView.AnimationCurve = .easeOut, | |
completion: (() -> Void)? = nil | |
) { | |
center = fromValue | |
let animator = UIViewPropertyAnimator(duration: duration, curve: curve) { [weak self] in | |
self?.center = toValue | |
} | |
animator.addCompletion { position in | |
switch position { | |
case .end: | |
completion?() | |
default: | |
break | |
} | |
} | |
animator.startAnimation(afterDelay: delay) | |
} | |
func colorant( | |
duration: CGFloat, | |
fromValue: UIColor, | |
toValue: UIColor, | |
delay: CGFloat = 0, | |
curve: UIView.AnimationCurve = .easeOut, | |
completion: (() -> Void)? = nil | |
) { | |
backgroundColor = fromValue | |
let animator = UIViewPropertyAnimator(duration: duration, curve: curve) { [weak self] in | |
self?.backgroundColor = toValue | |
} | |
animator.addCompletion { position in | |
switch position { | |
case .end: | |
completion?() | |
default: | |
break | |
} | |
} | |
animator.startAnimation(afterDelay: delay) | |
} | |
func transformer( | |
duration: CGFloat, | |
fromValue: CGAffineTransform, | |
toValue: CGAffineTransform, | |
delay: CGFloat = 0, | |
curve: UIView.AnimationCurve = .easeOut, | |
completion: (() -> Void)? = nil | |
) { | |
transform = fromValue | |
let animator = UIViewPropertyAnimator(duration: duration, curve: curve) { [weak self] in | |
self?.transform = toValue | |
} | |
animator.addCompletion { position in | |
switch position { | |
case .end: | |
completion?() | |
default: | |
break | |
} | |
} | |
animator.startAnimation(afterDelay: delay) | |
} | |
static func animator( | |
duration: CGFloat, | |
animation: @escaping () -> Void, | |
delay: CGFloat = 0, | |
curve: UIView.AnimationCurve = .easeOut, | |
completion: (() -> Void)? = nil | |
) { | |
let animator = UIViewPropertyAnimator(duration: duration, curve: curve) { | |
animation() | |
} | |
animator.addCompletion { position in | |
switch position { | |
case .end: | |
completion?() | |
default: | |
break | |
} | |
} | |
animator.startAnimation(afterDelay: delay) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment