Skip to content

Instantly share code, notes, and snippets.

@conath
Created June 9, 2020 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conath/ff9108491f111a72bb8ae81537c2ceb6 to your computer and use it in GitHub Desktop.
Save conath/ff9108491f111a72bb8ae81537c2ceb6 to your computer and use it in GitHub Desktop.
Shakes a UIView left and right, a good duration seems to be 0.15s
import UIKit
extension UIView {
func animateWiggle(withDuration duration: TimeInterval, delay: TimeInterval, completion: @escaping((Bool) -> ())) {
UIView.animate(withDuration: duration/4, delay: delay, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: {
self.transform = CGAffineTransform.identity.translatedBy(x: 8, y: 0)
}) { _ in
UIView.animate(withDuration: duration/4, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: {
self.transform = CGAffineTransform.identity.translatedBy(x: -7, y: 0)
}) { _ in
UIView.animate(withDuration: duration/8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: {
self.transform = CGAffineTransform.identity.translatedBy(x: 5, y: 0)
}) { _ in
UIView.animate(withDuration: duration/8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: {
self.transform = CGAffineTransform.identity.translatedBy(x: -3, y: 0)
}) { _ in
UIView.animate(withDuration: duration/4, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: {
self .transform = CGAffineTransform.identity
}, completion: completion)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment