我想在动画开始时观察 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)observeValueForKeyPathNSString *)keyPath ofObjectid)object changeNSDictionary *)change contextvoid *)context {
NSLog(@"change:%@",change);
// not called here...
}
Best Answer-推荐答案 strong>
当动画像这样“飞行”时,您无法观察到动画属性的变化。该属性实际上在动画开始时设置为结束值。然后有一个“表示层”放置在 View 的常规层之上,动画发生在该层上。
您能做的最好的事情是设置一个 CADisplayLink 计时器(一个与屏幕刷新率同步的轻量级计时器)并查询表示层上的属性因为它动画(layer.presentationLayer.strokeEnd ,在你的情况下。)
关于ios - 观察层 'strokeEnd' 动画事件,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/47824884/
|