Created
June 9, 2020 12:37
-
-
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
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
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