CAKeyframeAnimation有兩種添加動畫的方式
1.value方式
通過添加關(guān)鍵點(diǎn)描述動畫
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//設(shè)置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];
//重復(fù)次數(shù) 默認(rèn)1
animation.repeatCount = 1;
//設(shè)置動畫時間,時間內(nèi)動畫一直重復(fù)
//animation.repeatDuration = 60;
//是否原路返回 默認(rèn)NO
animation.autoreverses = NO;
//移動速度叶骨,越小越快
animation.duration = 4.0f;
//動畫速度變化
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//動畫結(jié)束時動作
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);
//重復(fù)次數(shù) 默認(rèn)1
animation.repeatCount = 1;
//設(shè)置動畫時間粹污,時間內(nèi)動畫一直重復(fù)
//animation.repeatDuration = 60;
//是否原路返回 默認(rèn)NO
animation.autoreverses = NO;
//移動速度桥温,越小越快
animation.duration = 4.0f;
//動畫速度變化
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//動畫結(jié)束時動作
animation.fillMode = kCAFillModeForwards;
//根據(jù)路徑自動旋轉(zhuǎn)
animation.rotationMode = kCAAnimationRotateAutoReverse;
animation.removedOnCompletion = NO;
[self.carImg.layer addAnimation:animation forKey:nil];