Created
September 7, 2012 16:03
-
-
Save pieteromvlee/3667417 to your computer and use it in GitHub Desktop.
ShakeAnimation; a variant on @danielctull's versopm
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
@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