Skip to content

Instantly share code, notes, and snippets.

@pieteromvlee
Created September 7, 2012 16:03
Show Gist options
  • Save pieteromvlee/3667417 to your computer and use it in GitHub Desktop.
Save pieteromvlee/3667417 to your computer and use it in GitHub Desktop.
ShakeAnimation; a variant on @danielctull's versopm
@implementation ShakeAnimation {
CGRect originalRect;
NSInteger counter;
}
+ (void)shakeView:(UIView *)view {
[[[[self class] alloc] initWithView:view] shake];
}
- (id)initWithView:(UIView *)view {
self = [super init];
if (self) {
self.view = view;
originalRect = view.frame;
counter = 20;
}
return self;
}
- (void)shake {
[self.view addFrameX:5];
[self shakeWithDirection:-1 completion:^{
self.view.frame = originalRect;
}];
}
- (void)shakeWithDirection:(NSInteger)flip completion:(dispatch_block_t)completion {
[UIView animateWithDuration:0.05f animations:^{
[self.view addFrameX:10 * flip];
} completion:^(BOOL finished){
counter--;
if (counter == 0)
completion();
else
[self shakeWithDirection:flip * -1 completion:completion];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment