Created
July 6, 2016 15:03
-
-
Save rogerluan/7c09afa1ef2be6456e4d1f1f556fbada to your computer and use it in GitHub Desktop.
A shake animation that resembles an error animation feedback. It happens once - it's not a constant wiggling like SpringBoard app edit wiggle animation.
This file contains hidden or 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
/** | |
* @Credits: paiego | |
* @Description: edited paiego's code to adapt to my use (error animation feedback) | |
* @See: http://stackoverflow.com/a/9753948/4075379 | |
*/ | |
- (CAAnimation *)shakeAnimation { | |
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; | |
CGFloat wobbleAngle = 0.06f; | |
NSValue *valLeft; | |
NSValue *valRight; | |
NSMutableArray *values = [NSMutableArray new]; | |
for (int i = 0; i < 5; i++) { | |
valLeft = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)]; | |
valRight = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)]; | |
[values addObjectsFromArray:@[valLeft, valRight]]; | |
wobbleAngle*=0.66; | |
} | |
animation.values = [values copy]; | |
animation.duration = 0.7; | |
return animation; | |
} | |
//Usage: | |
[your_view.layer addAnimation:[self shakeAnimation] forKey:@""]; //do the shake animation | |
your_view.transform = CGAffineTransformIdentity; //return the view back to original |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment