KVO on strokeEnd CASepeLayer

时间:2017-11-22 20:04:00

标签: ios swift

我有两层。 Layer ALayer B

我正在将strokeEnd的{​​{1}}从某个值设置为某个新值。我还需要同时更改Layer A上的内容,但它取决于Layer B Layer A的当前值,同时它是动画效果。有没有办法可以观察strokeEnd的表示层的strokeEnd?

1 个答案:

答案 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;
}