Skip to content

Instantly share code, notes, and snippets.

@lalkrishna
Created October 17, 2019 06:24
Show Gist options
  • Save lalkrishna/458da472010fe38dde87f07103023e57 to your computer and use it in GitHub Desktop.
Save lalkrishna/458da472010fe38dde87f07103023e57 to your computer and use it in GitHub Desktop.
UIView error shaking animation.
- (void)shake {
[self _shake:10 direction:1 currentTimes:0 withDelta:5 speed:0.03 shakeDirection:ShakeDirectionHorizontal completion:nil];
}
- (void)_shake:(int)times direction:(int)direction currentTimes:(int)current withDelta:(CGFloat)delta speed:(NSTimeInterval)interval shakeDirection:(ShakeDirection)shakeDirection completion:(void (^)(void))completionHandler {
__weak UIView *weakSelf = self;
[UIView animateWithDuration:interval animations:^{
weakSelf.layer.affineTransform = (shakeDirection == ShakeDirectionHorizontal) ? CGAffineTransformMakeTranslation(delta * direction, 0) : CGAffineTransformMakeTranslation(0, delta * direction);
} completion:^(BOOL finished) {
if(current >= times) {
[UIView animateWithDuration:interval animations:^{
weakSelf.layer.affineTransform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
if (completionHandler != nil) {
completionHandler();
}
}];
return;
}
[weakSelf _shake:(times - 1)
direction:direction * -1
currentTimes:current + 1
withDelta:delta
speed:interval
shakeDirection:shakeDirection
completion:completionHandler];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment