2010-06-09 24 views
0

嗨,我有兩個CALayers(* layer1和* layer2)。我想要的是當layer1的動畫時間的50%已經過去時,在layer2上開始動畫。我怎樣才能做到這一點?如何協調多個CALayers上的動畫時間?

我嘗試過使用CAAnimationGroup,但只適用於同一圖層中的動畫。我發現了一個解決方法,你必須首先將延遲動畫添加到組中,並設置beginTime屬性,但這對我來說似乎有些ha and,我想知道是否有正確的方法來實現我想要的。

謝謝!

回答

0

將第二個動畫添加到組的方法確實感覺像是一種黑客攻擊,但它的工作原理並且完全可以接受。但是,如果您願意,可以執行以下操作:-performSelector:withObject:afterDelay。類似這樣的:

- (void)startFirstAnimation; 
{ 
    CGFloat duration = 10.0; 

    CABasicAnimation *firstAnim = 
      [CABasicAnimation animationWithKeyPath:@"position"]; 
    [firstAnim setFromValue: 
      [NSValue valueWithCGPoint:CGPointMake(30.0f, 30.0f)]]; 
    [firstAnim setToValue: 
      [NSValue valueWithCGPoint:CGPointMake(200.0f, 200.0f)]]; 
    [firstAnim setDuration:duration]; 

    [firstLayer addAnimation:firstAnim forKey:nil]; 

    [self performSelector:@selector(startSecondAnimation) 
       withObject:nil afterDelay:duration/2.0]; 
} 

- (void)startSecondAnimation; 
{ 
    CABasicAnimation *secondAnim = 
      [CABasicAnimation animationWithKeyPath:@"position"]; 
    [secondAnim setFromValue: 
      [NSValue valueWithCGPoint:CGPointMake(100.0f, 30.0f)]]; 
    [secondAnim setToValue: 
      [NSValue valueWithCGPoint:CGPointMake(200.0f, 200.0f)]]; 
    [secondAnim setDuration:5.0]; 

    [secondLayer addAnimation:secondAnim forKey:nil]; 
}