CAKeyframeAnimation有两种添加动画的方式
1.value方式
通过添加关键点描述动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置value
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(self.roadView.frame.size.width/4*3, self.view.frame.size.height - kScaleY*30 - self.carImg.frame.size.height/2)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(self.roadView.frame.size.width/4*3, kScaleY*200)];
animation.values = @[value1,value2];
//重复次数 默认1
animation.repeatCount = 1;
//设置动画时间,时间内动画一直重复
//animation.repeatDuration = 60;
//是否原路返回 默认NO
animation.autoreverses = NO;
//移动速度,越小越快
animation.duration = 4.0f;
//动画速度变化
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//动画结束时动作
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
[self.carImg.layer addAnimation:animation forKey:nil];
2.path方式
通过添加路径描述动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//CGPathRef对象,动画的路线
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, NULL, CGRectMake(kScaleX*50, kScaleY*150, kScaleX*280, kScaleY*400));
animation.path = path;
CGPathRelease(path);
//重复次数 默认1
animation.repeatCount = 1;
//设置动画时间,时间内动画一直重复
//animation.repeatDuration = 60;
//是否原路返回 默认NO
animation.autoreverses = NO;
//移动速度,越小越快
animation.duration = 4.0f;
//动画速度变化
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//动画结束时动作
animation.fillMode = kCAFillModeForwards;
//根据路径自动旋转
animation.rotationMode = kCAAnimationRotateAutoReverse;
animation.removedOnCompletion = NO;
[self.carImg.layer addAnimation:animation forKey:nil];
网友评论