Skip to content

Instantly share code, notes, and snippets.

@jhildensperger
Created June 30, 2014 06:54
Show Gist options
  • Save jhildensperger/efcdbfc395343f4992d7 to your computer and use it in GitHub Desktop.
Save jhildensperger/efcdbfc395343f4992d7 to your computer and use it in GitHub Desktop.
Method to shake a view with a completion block
- (void)shakeView:(UIView *)view completion:(void (^)(void))completion {
[UIView animateKeyframesWithDuration:.5 delay:0 options:UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:.25 animations:^{
view.transform = CGAffineTransformMakeTranslation(-5, 0);
}];
[UIView addKeyframeWithRelativeStartTime:.25 relativeDuration:.25 animations:^{
view.transform = CGAffineTransformMakeTranslation(5, 0);
}];
[UIView addKeyframeWithRelativeStartTime:.5 relativeDuration:.25 animations:^{
view.transform = CGAffineTransformMakeTranslation(-5, 0);
}];
[UIView addKeyframeWithRelativeStartTime:.75 relativeDuration:.25 animations:^{
view.transform = CGAffineTransformMakeTranslation(0, 0);
}];
} completion:^(BOOL finished) {
if (finished && completion) {
completion();
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment