Skip to content

Instantly share code, notes, and snippets.

@mackross
Created February 3, 2012 03:50
Show Gist options
  • Save mackross/1727556 to your computer and use it in GitHub Desktop.
Save mackross/1727556 to your computer and use it in GitHub Desktop.
Core Animation Shake
- (void)shakeAnimation:(CALayer*)layer
{
CGPoint pos = layer.position;
static int numberOfShakes = 3;
static CGFloat vigourOfShake = 0.055;
CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef shakePath = CGPathCreateMutable();
CGPathMoveToPoint(shakePath, NULL, pos.x, pos.y);
int index;
for (index = 0; index < numberOfShakes; ++index)
{
CGPathAddLineToPoint(shakePath, NULL, pos.x - layer.frame.size.width * vigourOfShake, pos.y);
CGPathAddLineToPoint(shakePath, NULL, pos.x + layer.frame.size.width * vigourOfShake, pos.y);
}
CGPathAddLineToPoint(shakePath, NULL, pos.x, pos.y);
CGPathCloseSubpath(shakePath);
shakeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
shakeAnimation.duration = 0.65;
shakeAnimation.path = shakePath;
[layer addAnimation:shakeAnimation forKey:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment