Ios 观察第'层;strokeEnd';动画事件

Ios 观察第'层;strokeEnd';动画事件,ios,animation,observer-pattern,Ios,Animation,Observer Pattern,我希望在动画开始时观察strokeEnd关键点路径。但它不起作用,我哪里出错了 - (void)addAnimation { // do animation CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; drawAnimation.duration = 3.f; drawAnimation.repeatCount

我希望在动画开始时观察strokeEnd关键点路径。但它不起作用,我哪里出错了

- (void)addAnimation {
    // do animation
    CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    drawAnimation.duration            = 3.f;
    drawAnimation.repeatCount         = 1.0;
    drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    drawAnimation.toValue   = [NSNumber numberWithFloat:0.5f];
    drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    drawAnimation.fillMode = kCAFillModeForwards;
    drawAnimation.removedOnCompletion = NO;
    [self.progressLayer addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
    [self.progressLayer addObserver:self forKeyPath:@"strokeEnd" options:NSKeyValueObservingOptionNew context:NULL]; // 监听position

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"change:%@",change);
    // not called here...
}

当动画处于“飞行”状态时,您无法观察动画属性的变化。该特性实际上设置为动画开始时的结束值。然后是一个“表示层”,它位于视图的常规层之上,动画在该层上进行


您最好设置一个
CADisplayLink
计时器(一个与屏幕刷新率同步的轻量级计时器),并在演示层设置动画时查询该属性(
layer.presentationLayer.strokeEnd
,在您的情况下)。

我不确定是否会有任何区别,但请尝试将上下文设置为“nil”而不是NULL。。因为我在这里没有看到任何其他问题。你确定strokeEnd是KVO兼容的吗?我已经按照你的步骤进行了操作,一切正常!谢谢,我正在用
CADisplayLink
选择器指定的方法调用
layer.presentationLayer.strokeEnd
。它在
层.presentationLayer
上崩溃。我不知道为什么。错误消息是
-[CAShapeLayer doubleValue]:无法识别的选择器发送到实例0x283624c40