我有两层。 Layer A
和Layer B
。
我正在将strokeEnd
的{{1}}从某个值设置为某个新值。我还需要同时更改Layer A
上的内容,但它取决于Layer B
Layer A
的当前值,同时它是动画效果。有没有办法可以观察strokeEnd
的表示层的strokeEnd?
答案 0 :(得分:3)
也许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;
}