1

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

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

比如说:

  • anim1 -> 持续时间 5s
  • anim2 -> 3s
  • anim3 -> 10s

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

感谢帮助 !

4

2 回答 2

2

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

anim2.beginTime = anim1.beginTime + anim1.duration;
anim3.beginTime = anim2.beginTime + anim2.duration;
于 2011-04-08T17:08:21.433 回答
1

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

在 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 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
            }];
        }];
    }]; 

}

于 2014-04-11T10:15:11.050 回答