ios - CAShapeLayer 的 strokeEnd 上的 KVO

标签 ios swift

我有两层。 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/

相关文章:

ios - 如何使用自定义复选框按钮选择 TableView 行选择?

ios - 在第一个 View Controller 的标签中显示输入到文本字段(在第二个 View Controller 中)的文本?

ios - 使用 parse.com 设置关注者列表的最佳方法是什么

ios - 如何处理 Swift 中缺少反射的问题?

javascript - 无法使用 JavaScript 在 iphone 上播放声音但可以在 Android 上播放

ios - nil 导入带有电子邮件地址的联系人时验证失败

ios - 如何在 Swift 的数组中获取所有可用的表情符号?

swift - 从警告框中获取文本值

swift - 如何在 xcode 中多次运行函数?

ios - 如何在相机覆盖层的正中间设置十字线图标?