为什么 CATextLayer 对象的 CAKeyFrameAnimation 不一致?

发布于 2024-12-23 20:04:53 字数 1201 浏览 1 评论 0原文

目的是什么: 按钮单击请求 CATextLayer 对象的字符串值进行动画处理,直到被另一个按钮单击请求停止为止。有两个单独的按钮负责这些操作,以便操作不会混淆。

实际发生的情况: 在播放请求中,有多个实例,textLayer 的字符串没有动画,并且其字符串值保持不变,同时显示分配的“0”(如上所示)。在这种情况下,动画永远不会被初始化并分配给 CATextLayer 对象。 然而,错误并不一致。还有一些 CATextLayer 对象将显示适当的字符串值的情况。如果连续单击按钮,看到 CATextLayer 对象为其字符串值设置动画的成功率大约为 70%。是否有任何场景可以绕过 addAnimation 方法代码行?

我感谢您对问题的任何帮助。提前致谢!

-(IBAction)playText:(id)sender{
    //textLayer object is instantiated elsewhere in the class
    textLayer.frame = CGRectMake(0, 0, 128, 16);
    textLayer.fontSize = 14;
    textLayer.backgroundColor = [UIColor clearColor].CGColor;
    textLayer.foregroundColor = [UIColor yellowColor].CGColor;
    textLayer.string = @"0";
    [self.layer addSubLayer:textLayer];

    CAKeyframeAnimation *textAnimation = [CAKeyframeAnimation animationWithKeyPath:@"string"];
    textAnimation.values = values; 
    textAnimation.repeatCount = HUGE_VALF;
    textAnimation.keyTimes = intervals; 
    textAnimation.calculationMode = kCAAnimationLinear;
    textAnimation.duration = 6;
    [textLayer addAnimation:textAnimation forKey:@"string"];
}

-(IBAction)stopText:(id)sender{
    [textLayer removeAnimationForKey:@"string"];
}

What is intended:
A button click requests that a CATextLayer object's string value is animated until being stopped by another button click request. There are two separate buttons responsible for these actions so that the actions are not confused.

What actually happens:
On the play request, there are several instances when the textLayer's string is not animated, and its string value remains unchanged, while displaying the assigned "0" (as shown above). In this case, the animation is never initialized and assigned to the CATextLayer object.
However, the error is not consistent. There are also several instances when the CATextLayer object will display the appropriate string values. If continuously clicking the button, the success rate is roughly 70% of seeing the CATextLayer object animate its string value. Are there any scenarios which would bypass the the addAnimation method line of code?

I appreciate any and all of your help with issue. Thanks in advance!

-(IBAction)playText:(id)sender{
    //textLayer object is instantiated elsewhere in the class
    textLayer.frame = CGRectMake(0, 0, 128, 16);
    textLayer.fontSize = 14;
    textLayer.backgroundColor = [UIColor clearColor].CGColor;
    textLayer.foregroundColor = [UIColor yellowColor].CGColor;
    textLayer.string = @"0";
    [self.layer addSubLayer:textLayer];

    CAKeyframeAnimation *textAnimation = [CAKeyframeAnimation animationWithKeyPath:@"string"];
    textAnimation.values = values; 
    textAnimation.repeatCount = HUGE_VALF;
    textAnimation.keyTimes = intervals; 
    textAnimation.calculationMode = kCAAnimationLinear;
    textAnimation.duration = 6;
    [textLayer addAnimation:textAnimation forKey:@"string"];
}

-(IBAction)stopText:(id)sender{
    [textLayer removeAnimationForKey:@"string"];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2024-12-30 20:04:53

除了重新添加到其父子图层之外,还必须为每个关键帧动画重新​​初始化该对象。

The object must be re-initialized for each keyframe animation, in addition to be re-added to it's parent Sublayer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文