iOS Layer基本动画
一、基本动画
1、旋转
//旋转的时候可以绕着 x,y,z旋转 默认的是z
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
basicAnimation.duration = 5;
//保持最后的位置和动画结束的位置一致
basicAnimation.fillMode = kCAFillModeForwards;
basicAnimation.removedOnCompletion = NO;
//byvalue:最后的结果是 byValue + fromValue
//tovalue:最后的结果舐 tovalue
// basicAnimation.fromValue = @(M_PI / 8);
basicAnimation.toValue = @(M_PI);
// basicAnimation.byValue = @(M_PI / 8);
[_myView.layer addAnimation:basicAnimation forKey:@"basic"];
2、缩放
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
basicAnimation.duration = 5;
basicAnimation.fillMode = kCAFillModeBoth;
basicAnimation.removedOnCompletion = NO;
basicAnimation.delegate = self;
//byvalue:最后的结果是 byValue + fromValue
//tovalue:最后的结果是 tovalue
// basicAnimation.fromValue = @(2);
basicAnimation.toValue = @(0.5);
// basicAnimation.byValue = @(2);
[_myView.layer addAnimation:basicAnimation forKey:@"basic”];
//在动画执行结束之后,继续执行一些操作
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
二、group
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
basicAnimation.duration = 3;
basicAnimation.fillMode = kCAFillModeBoth;
basicAnimation.removedOnCompletion = NO;
basicAnimation.delegate = self;
//byvalue:最后的结果是 byValue + fromValue
//tovalue:最后的结果是 tovalue
// basicAnimation.fromValue = @(2);
basicAnimation.toValue = @(0.5);
// basicAnimation.byValue = @(2);
中心点旋转CGAffineTransformRotate
view.titleImg.transform=CGAffineTransformRotate(view.titleImg.transform,0.001);
返回原点
view.titleImg.transform=CGAffineTransformMakeRotation(0);
//旋转的时候可以绕着 x,y,z旋转 默认的是z
CABasicAnimation *basicAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
basicAnimation1.duration = 3;
//保持最后的位置和动画结束的位置一致
basicAnimation1.fillMode = kCAFillModeForwards;
basicAnimation1.removedOnCompletion = NO;
//byvalue:最后的结果是 byValue + fromValue
//tovalue:最后的结果舐 tovalue
// basicAnimation.fromValue = @(M_PI / 8);
basicAnimation1.toValue = @(M_PI);
// basicAnimation.byValue = @(M_PI / 8);
//group
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.duration = 3;
groupAnimation.repeatCount = 10;
//按照原路返回
groupAnimation.autoreverses = YES;
groupAnimation.animations = @[basicAnimation,basicAnimation1];
[_myView.layer addAnimation:groupAnimation forKey:@"eee”];
三、spring
CASpringAnimation *spring = [CASpringAnimation animationWithKeyPath:@"transform.scale"];
//质量
spring.mass = 50;
spring.fromValue = @(0.5);
spring.toValue = @(1);
spring.repeatCount = 10;
spring.autoreverses = YES;
spring.initialVelocity = 100;
spring.duration = spring.settlingDuration;
[_myView.layer addAnimation:spring forKey:@"spring”];
四、帧动画
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 200)];
[path addLineToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(100, 200)];
[path addLineToPoint:CGPointMake(150, 300)];
[path addLineToPoint:CGPointMake(100, 400)];
[path addLineToPoint:CGPointMake(100, 200)];
CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnimation.duration = 5;
moveAnimation.removedOnCompletion = NO;
moveAnimation.fillMode = kCAFillModeForwards;
//淡入淡出
moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//用路径 所以value无效
moveAnimation.path = path.CGPath;
[_myView.layer addAnimation:moveAnimation forKey:@"moveAnimation”];
五、value times
//values times
CAKeyframeAnimation *rotationAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.duration = 10;
rotationAnimation.values = @[@(0),@(M_PI /4),@(M_PI)];
rotationAnimation.keyTimes = @[@(0.1),@(0.3),@(0.8)];
[_myView.layer addAnimation:rotationAnimation forKey:@"ww"];