I think all this is dangerous.
If your shake animation is based on user action and that user action is triggered during the animation.
CRAAAAAASH
Here is my path in Swift 4 :
static func shake(view: UIView, for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) { let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) { view.transform = CGAffineTransform(translationX: translation, y: 0) } propertyAnimator.addAnimations({ view.transform = CGAffineTransform(translationX: 0, y: 0) }, delayFactor: 0.2) propertyAnimator.startAnimation() }
It may not be the cleanest, but this method can be run multiple times and is easy to understand
Edit:
I am a big proponent of using UIViewPropertyAnimator. So many interesting features that allow you to make dynamic changes to the main animations.
Here is another example to add a red border until the view shakes and then removes it when the shake ends.
static func shake(view: UIView, for duration: TimeInterval = 0.5, withTranslation translation: CGFloat = 10) { let propertyAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.3) { view.layer.borderColor = UIColor.red.cgColor view.layer.borderWidth = 1 view.transform = CGAffineTransform(translationX: translation, y: 0) } propertyAnimator.addAnimations({ view.transform = CGAffineTransform(translationX: 0, y: 0) }, delayFactor: 0.2) propertyAnimator.addCompletion { (_) in view.layer.borderWidth = 0 } propertyAnimator.startAnimation() }
Corey Pett Apr 28 '18 at 18:54 2018-04-28 18:54
source share