Created
December 3, 2021 19:33
-
-
Save erdemildiz/8c1c4db2bfabc866343dce21b1d78c98 to your computer and use it in GitHub Desktop.
UIView+Animation
This file contains hidden or 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
// 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