我有两层。 A 层
和B 层
。
我正在将 A 层
的 strokeEnd
从某个值设置为某个新值的动画。我还需要同时更改 Layer B
上的某些内容,但这取决于 Layer A
在设置动画时 strokeEnd
的当前值。有什么方法可以观察 A 层
的表示层的 strokeEnd?
最佳答案
也许CADisplayLink
就是您需要的答案。我的项目中有一些代码,仅供引用。
CABasicAnimation *progressAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
progressAnimation.duration = self.animationDuration;
progressAnimation.fromValue = @0.0f;
progressAnimation.toValue = @(progress);
progressAnimation.delegate = self;
[self.progressLayer addAnimation:progressAnimation forKey:@"strokeEndAnimation"];
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateVipTagLabelAndBtn)];
self.displayLink.paused = YES;
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
- (void)updateVipTagLabelAndBtn {
CAShapeLayer *layer = (CAShapeLayer *)[self.progressLayer presentationLayer];
CGFloat strokeEnd = [[layer valueForKeyPath:@"strokeEnd"] floatValue];
// do your work here
}
- (void)animationDidStart:(CAAnimation *)anim {
self.displayLink.paused = NO;
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
self.displayLink.paused = YES;
[self.displayLink invalidate];
self.displayLink = nil;
}
关于ios - CAShapeLayer 的 strokeEnd 上的 KVO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47443052/