views:

53

answers:

1

Hi I have two CALayers (*layer1 and *layer2). What I want is to start the animation on layer2 when 50% of the layer1's animation time has elapsed. How can I achieve that?

I tried using CAAnimationGroup but that works only for animations in the same layer. I have found a workaround in which you have to add the delayed animation to a group first and set the beginTime property but this seems a little hackish to me and I would like to know if there is a right way of achieving what I want.

Thank you!

A: 

The method of adding the second animation to a group does feel like a bit of a hack, but it works and is perfectly acceptable. What you could do instead if you prefer, though, is call -performSelector:withObject:afterDelay. Something like this:

- (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];
}
Matt Long