Multiple CAKeyframeAnimation in different layers at the same time

Anyone now, how can I animate multiple layers at once using CAKeyframeAnimation? Each layer has its own CAKeyframeAnimation object. Take a look at the code below:

I have a method that receives an object, creates a CAKeyframeAnimation and attaches the animation to it:

- (void)animateMovingObject:(CALayer*)obj
               fromPosition:(CGPoint)startPosition
                 toPosition:(CGPoint)endPosition
                   duration:(NSTimeInterval)duration {
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    //pathAnimation.fillMode = kkCAFillModeRemoved; // default 
    //pathAnimation.removedOnCompletion = YES; // default
    pathAnimation.duration = duration;

    // create an empty mutable path
    CGMutablePathRef curvedPath = CGPathCreateMutable();

    // set the starting point of the path
    CGPathMoveToPoint(curvedPath, NULL, startPosition.x, startPosition.y);

    CGPathAddCurveToPoint(curvedPath, NULL, 
                          startPosition.x, endPosition.y, 
                          startPosition.x, endPosition.y,
                          endPosition.x, endPosition.y);
    pathAnimation.path = curvedPath;
    [obj addAnimation:pathAnimation forKey:@"pathAnimation"];
    CGPathRelease(curvedPath);
}

Now suppose I have 3 layers added as a sublevel in my board game, and I make the following calls:

CALayer obj1 = ... // set up layer and add as sublayer
[self animateMovingObject:obj1
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(100.0, 100.0)
                 duration:2.0];

CALayer obj2 = ... // set up layer and add as sublayer
[self animateMovingObject:obj2
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(150.0, 100.0)
                 duration:2.0];

CALayer obj3 = ... // set up layer and add as sublayer
[self animateMovingObject:obj3
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(200.0, 100.0)
                 duration:2.0];

Thus, I see that it is only obj3moving from position (0.0, 0.0) to (200.0, 100.0). What am I missing? Should I use NSOperationQueue / Threads? Using the delegate animationDidStart:method of the CAKeyframeAnimation method does not seem useful in this context.

Any ideas?

Thanks in advance.

+5
1

, , , , CATransaction:

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];     

// Do animations

[CATransaction commit];     

, , .

, , removedOnCompletion YES, , . , , .

+4

All Articles