- (void)shake{
CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
moveAnimation.byValue = [NSNumber numberWithFloat:50.0];
// Here's the important part
[moveAnimation setDuration:0.1];
[moveAnimation setBeginTime:0.0];
moveAnimation.fillMode = kCAFillModeForwards;
CABasicAnimation *borderWidthAnimation = [CABasicAnimation animationWithKeyPath:@"borderWidth"];
[borderWidthAnimation setFromValue:[NSNumber numberWithFloat:0.0]];
[borderWidthAnimation setToValue:[NSNumber numberWithFloat:20.0]];
// Here's the important part
[borderWidthAnimation setDuration:10.0];
[borderWidthAnimation setBeginTime:5.0];
//kCAMediaTimingFunctionLinear kCAMediaTimingFunctionEaseInEaseOut
borderWidthAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"position.x"];
[springAnimation setDuration: 1.0];
[springAnimation setBeginTime:0.1];
springAnimation.stiffness = 100;
springAnimation.damping = 10;
springAnimation.initialVelocity = 15.0;
springAnimation.byValue = [NSNumber numberWithFloat:-50.0];
CAAnimationGroup *group = [CAAnimationGroup animation];
[group setDuration:1.0];
[group setAnimations:[NSArray arrayWithObjects:moveAnimation, springAnimation, nil]];
[self.ViewTest.layer addAnimation:group forKey:nil];
self.ViewTest.layer.borderColor = [UIColor yellowColor].CGColor;
}
//方法二:
// CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
shakeAnimation.duration = 0.25f;
// shakeAnimation.duration = 0.5f;
shakeAnimation.fromValue = [NSNumber numberWithFloat:-5];
shakeAnimation.toValue = [NSNumber numberWithFloat:5];
shakeAnimation.autoreverses = YES;
[_imageView.layer addAnimation:shakeAnimation forKey:nil];