Skip to content

Instantly share code, notes, and snippets.

@erdemildiz
Created December 3, 2021 19:33
Show Gist options
  • Save erdemildiz/8c1c4db2bfabc866343dce21b1d78c98 to your computer and use it in GitHub Desktop.
Save erdemildiz/8c1c4db2bfabc866343dce21b1d78c98 to your computer and use it in GitHub Desktop.
UIView+Animation
// Source: https://medium.com/nice-photon-ios/animator-easy-trick-to-make-uikit-animations-reusable-2d10713ca3a
struct Animator {
typealias Animations = () -> ()
typealias Completion = (Bool) -> ()
let perform: (@escaping Animations, Completion?) -> ()
}
extension UIView {
static func animate(with animator: Animator, animations: @escaping () -> (), completion: ((Bool) -> ())? = nil) {
animator.perform(animations, completion)
}
}
extension Animator {
static let defaultSpring = Animator { (animations, completion) in
UIView.animate(
withDuration: 0.4,
delay: 0,
usingSpringWithDamping: 0.8,
initialSpringVelocity: 1.75,
options: [.curveEaseInOut, .beginFromCurrentState, .allowUserInteraction],
animations: animations,
completion: completion
)
}
}
// Usage:
UIView.animate(with: .defaultSpring) {
self.view.center.y += 50
} completion: { (isCompleted) in
// completion code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment