Skip to content

Instantly share code, notes, and snippets.

@dabing1022
Forked from nielsbot/ShakeAnimation.m
Last active August 29, 2015 14:25
Show Gist options
  • Save dabing1022/0ad79b6a8736e8d849b5 to your computer and use it in GitHub Desktop.
Save dabing1022/0ad79b6a8736e8d849b5 to your computer and use it in GitHub Desktop.
// this code will cause a UIView to shake--good for "login error"
CAKeyframeAnimation * anim = [ CAKeyframeAnimation animationWithKeyPath:@"transform" ] ;
anim.values = [ NSArray arrayWithObjects:
[ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(-5.0f, 0.0f, 0.0f) ],
[ NSValue valueWithCATransform3D:CATransform3DMakeTranslation(5.0f, 0.0f, 0.0f) ],
nil ] ;
anim.autoreverses = YES ;
anim.repeatCount = 2.0f ;
anim.duration = 0.07f ;
[ viewToShake.layer addAnimation:anim forKey:nil ] ;
@dabing1022
Copy link
Author

#import <QuartzCore/QuartzCore.h>
CABasicAnimation *animation = 
                         [CABasicAnimation animationWithKeyPath:@"position"];
[animation setDuration:0.05];
[animation setRepeatCount:8];
[animation setAutoreverses:YES];
[animation setFromValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView center].x - 20.0f, [lockView center].y)]];
[animation setToValue:[NSValue valueWithCGPoint:
               CGPointMake([lockView center].x + 20.0f, [lockView center].y)]];
[[lockView layer] addAnimation:animation forKey:@"position"];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment