如何在多个CALayers上协调动画时间?

时间:2010-06-09 18:42:17

标签: iphone core-animation

您好我有两个CALayers(* layer1和* layer2)。我想要的是当layer1的动画时间已经过去50%时在layer2上启动动画。我怎样才能做到这一点?

我尝试使用CAAnimationGroup,但仅适用于同一图层中的动画。我找到了一个解决方法,你必须首先将延迟动画添加到一个组并设置beginTime属性,但这对我来说似乎有些晦涩,我想知道是否有正确的方法来实现我想要的。< / p>

谢谢!

1 个答案:

答案 0 :(得分: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];
}
相关问题