1 //左右震动效果 2 - (void)shake:(UIView *)view { 3 CGRect frame = view.frame; 4 CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 5 6 CGMutablePathRef shakePath = CGPathCreateMutable(); 7 CGPathMoveToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2, frame.origin.y+frame.size.height/2); 8 int index; 9 for (index = 3; index >=0; --index) { 10 CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 - frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2); 11 CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 + frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2); 12 } 13 CGPathCloseSubpath(shakePath); 14 15 shakeAnimation.path = shakePath; 16 shakeAnimation.duration = 0.5f; 17 shakeAnimation.removedOnCompletion = YES; 18 19 [view.layer addAnimation:shakeAnimation forKey:nil]; 20 CFRelease(shakePath); 21 } 22 23