按顺序组合多个 CAAnimation

发布于 2024-10-17 10:08:04 字数 357 浏览 7 评论 0原文

我正在阅读有关 CATransactions 的内容,然后认为这可能有助于解决我的问题。

这就是我不想做的: 我在同一层中有 3 个动画,它们都有自己的持续时间。我使用 CAKeyframeAnimation 和 CGMutablePathRef 创建动画。

例如:

  • anim1 ->持续时间 5 秒的
  • 动画2 -> 3s
  • 动画3 -> 10s

现在我想按顺序序列化它们。我尝试使用 CAAnimationGroup 但动画同时运行。 我读到了 CATransaction,这是一个可能的解决方案吗?你能给我举个小例子吗?

感谢您的帮助!

I was reading about CATransactions and then thought this might help solve my problem.

This is what I wan't to do:
I have 3 animations in the same layer, which all have their own duration. I create the animations using CAKeyframeAnimation with a CGMutablePathRef.

say for example:

  • anim1 -> duration 5s
  • anim2 -> 3s
  • anim3 -> 10s

Now I want to serialize them sequentially. I tried to use CAAnimationGroup but the animations run concurrently.
I read about CATransaction, is it a possible solution? Can you give me a little example?

thanks for help !

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

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

发布评论

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

评论(2

海的爱人是光 2024-10-24 10:08:04

如果序列化是指在前一个动画完成后开始每个动画,请使用 beginTime 属性(在 CAMediaTiming 协议中定义)。请注意,它的文档有点误导。这是一个例子:

anim2.beginTime = anim1.beginTime + anim1.duration;
anim3.beginTime = anim2.beginTime + anim2.duration;

If by serialization you mean starting each animation after the previous one has finished, use the beginTime property (defined in CAMediaTiming protocol). Note that its documentation is a little misleading. Here's an example:

anim2.beginTime = anim1.beginTime + anim1.duration;
anim3.beginTime = anim2.beginTime + anim2.duration;
ゃ人海孤独症 2024-10-24 10:08:04

如果您确定使用图层执行此操作,那么您可以尝试按照

在 CATransactions 中使用完成块

-(void)animateThreeAnimationsOnLayer:(CALayer*)layer animation:(CABasicAnimation*)firstOne animation:(CABasicAnimation*)secondOne animation:(CABasicAnimation*)thirdOne{
    [CATransaction begin];

        [CATransaction setCompletionBlock:^{
            [CATransaction begin];

            [CATransaction setCompletionBlock:^{
                [CATransaction begin];

                [CATransaction setCompletionBlock:^{
                    //If any thing on completion of all animations
                }];
                [layer addAnimation:thirdOne forKey:@"thirdAnimation"];
                [CATransaction commit];
            }];
            [layer addAnimation:secondOne forKey:@"secondAnimation"];
            [CATransaction commit];
        }];
    [layer addAnimation:firstOne forKey:@"firstAnimation"];
    [CATransaction commit];

}

另一种方法是通过应用延迟来开始动画。

-(void)animateThreeAnimation:(CALayer*)layer animation:(CABasicAnimation*)firstOne animation:(CABasicAnimation*)secondOne animation:(CABasicAnimation*)thirdOne{
    firstOne.beginTime=0.0;
    secondOne.beginTime=firstOne.duration;
    thirdOne.beginTime=firstOne.duration+secondOne.duration;

    [layer addAnimation:firstOne forKey:@"firstAnim"];
    [layer addAnimation:secondOne forKey:@"secondAnim"];
    [layer addAnimation:thirdOne forKey:@"thirdAnim"];
}

如果您将使用 UIVIew 动画

//if View is applicable in your requirement then you can look this one;
-(void)animateThreeAnimationOnView{
    [UIView animateWithDuration:2.0 animations:^{
        //first Animation
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2.0 animations:^{
            //Second Animation
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:2.0 animations:^{
                //Third Animation
            }];
        }];
    }]; 

}

If You are sure to do this thing with Layers then you may try like following

Using Completion Block in CATransactions

-(void)animateThreeAnimationsOnLayer:(CALayer*)layer animation:(CABasicAnimation*)firstOne animation:(CABasicAnimation*)secondOne animation:(CABasicAnimation*)thirdOne{
    [CATransaction begin];

        [CATransaction setCompletionBlock:^{
            [CATransaction begin];

            [CATransaction setCompletionBlock:^{
                [CATransaction begin];

                [CATransaction setCompletionBlock:^{
                    //If any thing on completion of all animations
                }];
                [layer addAnimation:thirdOne forKey:@"thirdAnimation"];
                [CATransaction commit];
            }];
            [layer addAnimation:secondOne forKey:@"secondAnimation"];
            [CATransaction commit];
        }];
    [layer addAnimation:firstOne forKey:@"firstAnimation"];
    [CATransaction commit];

}

Another way is by applying delay to begin animation.

-(void)animateThreeAnimation:(CALayer*)layer animation:(CABasicAnimation*)firstOne animation:(CABasicAnimation*)secondOne animation:(CABasicAnimation*)thirdOne{
    firstOne.beginTime=0.0;
    secondOne.beginTime=firstOne.duration;
    thirdOne.beginTime=firstOne.duration+secondOne.duration;

    [layer addAnimation:firstOne forKey:@"firstAnim"];
    [layer addAnimation:secondOne forKey:@"secondAnim"];
    [layer addAnimation:thirdOne forKey:@"thirdAnim"];
}

And if You are going to use UIVIew Animation

//if View is applicable in your requirement then you can look this one;
-(void)animateThreeAnimationOnView{
    [UIView animateWithDuration:2.0 animations:^{
        //first Animation
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2.0 animations:^{
            //Second Animation
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:2.0 animations:^{
                //Third Animation
            }];
        }];
    }]; 

}

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