CAKeyFrameAnimation 表示层似乎没有改变

CAKeyFrameAnimation presentation layer seems not to change

我正在使用 CAKeyframeAnimation 在圆形轨迹上移动 CALayer。有时我需要停止动画并将动画移动到动画停止的位置。这里的代码:

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.那么有没有其他方法可以找到动画层的当前位置?

我不知道表示层的位置 属性 不再告诉您层在 iOS 8 中的位置。这很有趣。在表示层上使用 hitTest 方法进行命中测试仍然有效,我只是在前一段时间写的应用程序上尝试过。

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

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

对于更复杂的路径,您必须了解图层所经过的路径。随着时间的推移绘制单个三次或二次贝塞尔曲线非常简单,但是复杂的 UIBezierPath/CGPath 混合了不同的形状将很难弄清楚。