CABasicAnimation 在开始之前停止?

发布于 2024-12-05 10:01:52 字数 710 浏览 0 评论 0原文

我需要淡入我的 CALayers。这是我的代码:

    for(int i = 0; i < viewArray.count; i++)
    {
        CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"];
        fadeIn.duration = 0.3;
        fadeIn.beginTime = i;
        fadeIn.fromValue = [NSNumber numberWithFloat:0.0];
        fadeIn.toValue = [NSNumber numberWithFloat:0.8];
        fadeIn.removedOnCompletion = NO;
        fadeIn.delegate = self;
        [((CALayer*)[viewArray objectAtIndex:i]) addAnimation:fadeIn forKey:nil];
    }

但是,只有前两个对象正确褪色,所有其他对象根本不褪色。我注意到,当我在animationDidStop 处放置断点时,大多数尚未开始的动画已经停止(即使是应该在 60 秒内开始的动画也会在前几秒内停止)。我不太确定发生了什么事。当我手动将每个 CALayer 的不透明度设置为 1 时,我可以正确看到它,但在制作动画时却看不到它。

I need to fade in my CALayers. Here's my code:

    for(int i = 0; i < viewArray.count; i++)
    {
        CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"];
        fadeIn.duration = 0.3;
        fadeIn.beginTime = i;
        fadeIn.fromValue = [NSNumber numberWithFloat:0.0];
        fadeIn.toValue = [NSNumber numberWithFloat:0.8];
        fadeIn.removedOnCompletion = NO;
        fadeIn.delegate = self;
        [((CALayer*)[viewArray objectAtIndex:i]) addAnimation:fadeIn forKey:nil];
    }

However, only the first two objects properly fades, all other objects do not fade at all. I've noticed that when I put a breakpoint at animationDidStop, most of the animations that have not started already stopped (even the animations that are supposed to start 60 seconds in are stopped within the first couple of seconds). I'm not exactly sure what's going on. I can see it properly when I manually set each CALayer's opacity to 1 but not when animating.

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

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

发布评论

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

评论(1

无声静候 2024-12-12 10:01:52

您将同时启动所有动画,因为对 addAnimation:forKey: 的调用不会同步等待动画完成。

我已经检查过了,看来你必须按以下方式设置 beginTime

fadeIn.beginTime = CACurrentMediaTime() + i;

You are starting all of the animations at the same time, because the call to addAnimation:forKey: does not synchronously wait until the animation is complete.

I have checked it and it seems you have to set the beginTime in a following way

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