2011-02-11 47 views
1

我在阅读有关CATransactions的内容,然后认为这可能有助于解决我的问题。按顺序组合多个CAAnimation

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

说,例如:

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

现在我要按顺序序列化。我试图使用CAAnimationGroup,但同时运行动画。 我阅读了关于CATransaction的信息,这是一个可行的解决方案吗?你能给我一个小例子吗?

感谢您的帮助!

回答

2

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

anim2.beginTime = anim1.beginTime + anim1.duration; 
anim3.beginTime = anim2.beginTime + anim2.duration; 
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 
      }]; 
     }]; 
    }]; 

}