Created
February 3, 2012 03:50
-
-
Save mackross/1727556 to your computer and use it in GitHub Desktop.
Core Animation Shake
This file contains 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
- (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