Ios7 在特定UIBezierPath上具有不同持续时间的CAKeyFrameAnimation?

Ios7 在特定UIBezierPath上具有不同持续时间的CAKeyFrameAnimation?,ios7,xcode5,core-animation,uibezierpath,Ios7,Xcode5,Core Animation,Uibezierpath,我想在UIBezierPath上设置一个图像的动画,持续时间为12秒。我用的是CAKeyframeAnimation。我的问题是,我想从起点到第一点动画2秒,从第一点到第二点动画4秒,从第二点到第三点动画4秒,从第三点到终点动画2秒。我该怎么做?这是我的密码 CGPoint startingpoint = CGPointMake(self.bounds.origin.x + 98, self.bounds.size.height -20); CGPoint firstPoint = CGPoin

我想在UIBezierPath上设置一个图像的动画,持续时间为12秒。我用的是CAKeyframeAnimation。我的问题是,我想从起点到第一点动画2秒,从第一点到第二点动画4秒,从第二点到第三点动画4秒,从第三点到终点动画2秒。我该怎么做?这是我的密码

CGPoint startingpoint = CGPointMake(self.bounds.origin.x + 98, self.bounds.size.height -20);
CGPoint firstPoint = CGPointMake(startingpoint.x + 20,startingpoint.y);
CGPoint secondpoint = CGPointMake(firstPoint.x + 30,firstPoint.y - 80);
CGPoint thirdpoint = CGPointMake(secondpoint.x + 50, secondpoint.y + 80);
CGPoint endPoints = CGPointMake(thirdpoint.x + 20,thirdpoint.y);

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startingpoint];
[path addLineToPoint:firstPoint];
[path addLineToPoint:secondpoint];
[path addLineToPoint:thirdpoint];
[path addLineToPoint:endPoints];

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor whiteColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[self.layer addSublayer:shapeLayer];

CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation1.duration = 10;
animation1.path = path.CGPath;
animation1.autoreverses = NO;
animation1.repeatCount = 0;
[myImageView.layer addAnimation:animation1 forKey:@"position"];

动画中缺少一些内容。具体来说,您没有在
CAKeyframeAnimation
上设置任何
关键时刻或值。即使它们是可选的,核心动画也不知道您希望在2秒后更改值,然后在4秒后再次更改。你必须告诉动画你想要什么

为此,您需要两个长度相等的
NSArray
s。第一个将关键帧的时间作为动画总时间的一个因素:

另一个将是一个值列表(在本例中,CGPoint包装在NSValues中,因为您正在设置CGPoint类型属性的动画),这些
keyTimes
将与之对应:

NSArray * pointValues = @[[NSValue valueWithCGPoint:startingPoint], [NSValue valueWithCGPoint:firstPoint], [NSValue valueWithCGPoint:secondPoint], [NSValue valueWithCGPoint:thirdPoint], [NSValue valueWithCGPoint:endPoint]];
最后在动画上设置它们:

animation1.keyTimes = keyTimes;
animation1.values = values;
如果您想获得真正的乐趣,还可以指定一个n-1长度的计时函数数组,用于在提供的关键时间之间对每个值更改进行计时:

CAMediaTimingFunction * linear = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CAMediaTimingFunction * easeIn = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CAMediaTimingFunction * fastEaseInOut = [CAMediaTimingFunction functionWithControlPoints:0.30f :0.92f :0.86f :0.95f];

NSArray * timingFunctions = @[easeIn, linear, linear, fastEaseInOut];
animation1.timingFunctions = timingFunctions;
CAMediaTimingFunction * linear = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CAMediaTimingFunction * easeIn = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CAMediaTimingFunction * fastEaseInOut = [CAMediaTimingFunction functionWithControlPoints:0.30f :0.92f :0.86f :0.95f];

NSArray * timingFunctions = @[easeIn, linear, linear, fastEaseInOut];
animation1.timingFunctions = timingFunctions;