Skip to content

Instantly share code, notes, and snippets.

@willrust
Created December 14, 2014 00:54
Show Gist options
  • Save willrust/3e87a8c44adc0f6bcd20 to your computer and use it in GitHub Desktop.
Save willrust/3e87a8c44adc0f6bcd20 to your computer and use it in GitHub Desktop.
Shake Animation
func shakeView(view: UIView, completion:((success: Bool) -> Void)?) {
UIView.animateKeyframesWithDuration(0.5,
delay: 0,
options: UIViewKeyframeAnimationOptions.BeginFromCurrentState,
animations: {
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1/4, animations: {
view.transform = CGAffineTransformMakeTranslation(-5, 0)
})
UIView.addKeyframeWithRelativeStartTime(1/4, relativeDuration: 1/4, animations: {
view.transform = CGAffineTransformMakeTranslation(5, 0)
})
UIView.addKeyframeWithRelativeStartTime(2/4, relativeDuration: 1/4, animations: {
view.transform = CGAffineTransformMakeTranslation(-5, 0)
})
UIView.addKeyframeWithRelativeStartTime(3/4, relativeDuration: 1/4, animations: {
view.transform = CGAffineTransformMakeTranslation(5, 0)
})
},
completion: { (success: Bool) -> Void in
completion!(success: success)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment