Skip to content

Instantly share code, notes, and snippets.

@dvlprliu
Last active August 29, 2015 13:57
Show Gist options
  • Save dvlprliu/9541633 to your computer and use it in GitHub Desktop.
Save dvlprliu/9541633 to your computer and use it in GitHub Desktop.
shake animation
- (void)shakenAView:(UIView *)view {
CGPoint leftPoint = CGPointMake(view.center.x - 3, view.center.y);
CGPoint rightPoint = CGPointMake(view.center.x + 3, view.center.y);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, view.center.x, view.center.y);
CGPathAddLineToPoint(path, NULL, leftPoint.x, leftPoint.y);
CGPathAddLineToPoint(path, NULL, rightPoint.x, rightPoint.y);
CGPathAddLineToPoint(path, NULL, leftPoint.x, rightPoint.y);
CGPathAddLineToPoint(path, NULL, view.center.x, view.center.y);
CAKeyframeAnimation *shakeAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
shakeAni.path = path;
shakeAni.duration = 0.3;
[view.layer addAnimation:shakeAni forKey:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment