Skip to content

Instantly share code, notes, and snippets.

@mlabraca
Created October 23, 2014 14:03
Show Gist options
  • Save mlabraca/b5d9c6bd91f3bd41290b to your computer and use it in GitHub Desktop.
Save mlabraca/b5d9c6bd91f3bd41290b to your computer and use it in GitHub Desktop.
- (void) startShake:(UIView*)view
{
CGAffineTransform leftShake = CGAffineTransformMakeTranslation(-5, 0);
CGAffineTransform rightShake = CGAffineTransformMakeTranslation(5, 0);
view.transform = leftShake; // starting point
[UIView beginAnimations:@"shake_view" context:view];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:5];
[UIView setAnimationDuration:0.06];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(shakeEnded:finished:context:)];
view.transform = rightShake; // end here & auto-reverse
[UIView commitAnimations];
}
- (void) shakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([finished boolValue]) {
UIView* item = (UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment