Created
October 23, 2014 14:03
-
-
Save mlabraca/b5d9c6bd91f3bd41290b to your computer and use it in GitHub Desktop.
Shake Animation for a given view in iOS. (source: http://2cupsoftech.wordpress.com/2012/09/20/shake-animation-to-a-uiview-or-uibutton/)
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
- (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