按顺序组合多个CAAnimation

时间:2011-02-11 21:16:37

标签: serialization caanimation cgpath catransaction

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

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

比如说:

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

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

如果通过序列化意味着在上一个动画完成之后启动每个动画,请使用beginTime属性(在CAMediaTiming协议中定义)。请注意,其文档有点误导。这是一个例子:

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

答案 1 :(得分:0)

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

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

}