2015-04-04 45 views
0

我正在使用CAKeyframeAnimation将CALayer移动到圆形轨迹上。有时我需要停止动画并将动画移动到动画停止点。这里代码:CAKeyFrameAnimation表示层似乎不会改变

CAKeyframeAnimation* circlePathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
CGMutablePathRef circularPath = the circle path; 

circlePathAnimation.path = circularPath; 
circlePathAnimation.delegate = self; 
circlePathAnimation.duration = 3.0f; 
circlePathAnimation.repeatCount = 1; 
circlePathAnimation.fillMode = kCAFillModeForwards; 
circlePathAnimation.removedOnCompletion = NO; 
circlePathAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:.43 :.78 :.69 :.99]; 
[circlePathAnimation setValue:layer forKey:@"animationLayer"]; 
[layer addAnimation:circlePathAnimation forKey:@"Rotation"]; 


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 
{ 
    CALayer *layer = [anim valueForKey:@"animationLayer"]; 
    if (layer) { 
     CALayer *presentationLayer = layer.presentationLayer; 
     layer.position = presentationLayer.position; 
    } 
} 

但是表示层没有位置变化!!!我读到它从ios 8中不再可靠。那么有没有其他方法可以找到动画层的当前位置?

+1

我已经获得了在iOS 8中使用表示层的动画对象的位置(实际上,frame.origin),并且它似乎工作正常。你能提供一个你读到的地方的参考,它不再可靠吗?我用presentationLayer.position对它进行了测试,结果也起作用。 – rdelmar 2015-04-05 00:24:42

回答

1

我不知道表示层的位置属性停止告诉你在iOS 8中你的图层的位置。这很有趣。在表现层上使用hitTest方法进行命中测试仍然有效,但我只是在一个我后来写过的应用程序上尝试过。

暂停和恢复动画也仍然有效。

对于像圆形路径这样的简单路径,您可以检查动画的时间,然后使用trig来计算位置。

对于更复杂的路径,您必须了解图层遍历的路径。随着时间的推移绘制单个三次或二次贝塞尔曲线非常简单,但复杂的UIBezierPath/CGPath混合了不同的形状,这将是一个难以解决的问题。