views:

45

answers:

2

I try to do a CAKeyFrameAnimation for rotating an layer:

CALayer* theLayer = myView.layer;
    CAKeyframeAnimation* animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

    animation.duration = 1.0;
    animation.cumulative = NO;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;

    animation.values = [NSArray arrayWithObjects:
                        [NSNumber numberWithFloat:0.0 * M_PI],
                        [NSNumber numberWithFloat:0.5 * M_PI],
                        [NSNumber numberWithFloat:0.3 * M_PI], // animation stops here...
                        [NSNumber numberWithFloat:0.8 * M_PI], // ignored!
                        [NSNumber numberWithFloat:0.7 * M_PI], nil]; // ignored!

    animation.keyTimes = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0],
                          [NSNumber numberWithFloat:0.2],
                          [NSNumber numberWithFloat:2.0], // ignored!
                          [NSNumber numberWithFloat:1.5], // ignored!
                          [NSNumber numberWithFloat:2], nil]; // ignored!

    animation.timingFunctions = [NSArray arrayWithObjects:
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], 
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], nil];

    [theLayer addAnimation:animation forKey:@"transform.rotation.z"];

Like you can see in the comments, the animation only runs two of the key frames, but not all of them. No matter what kind of values I put in there, the animation will never run more than two key frames.

What could be wrong there?

+2  A: 

Could it be that you set the animation.duration to 1 and yet your keyTimes are 0, 0.2 and then 2... meaning that the animation will stop before it reaches your 3rd value.

cp21yos
indeed it could! that was the "bug". thanks
openfrog
+1  A: 

The keyTimes array must only contain increasing values from 0.0 to 1.0. These are percentages of the progress through the animation, not raw numbers in seconds. So, the keyframe matching keyTime 0.5 will happen halfway through the animation, not 1/2 second into the animation (unless, of course, you have a one-second animation).

MikeyWard