Skip to content

Instantly share code, notes, and snippets.

@rogerluan
Created July 6, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerluan/7c09afa1ef2be6456e4d1f1f556fbada to your computer and use it in GitHub Desktop.
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.
/**
* @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