Search code examples
core-animationcakeyframeanimation

CAKeyframeAnimation


Hi i'm creating a Keyframe animation from multiple images. My problem is i would like the animation to instantly change from one image to the next rather than a fade.

    CALayer *animLayer = [CALayer layer];
    animLayer.bounds = CGRectMake(0, 0, width, height);
    animLayer.position = CGPointMake(0, 0);

    CAKeyframeAnimation *customFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    NSArray *sizeValues = [NSArray arrayWithObjects:(id)image1, (id)image2, nil];
    NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.5f], nil]; 
    NSArray *timingFunctions = [NSArray arrayWithObjects: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault], nil];

    [customFrameAnimation setValues:sizeValues];
    [customFrameAnimation setKeyTimes:times];

    customFrameAnimation.duration=5.0;
    customFrameAnimation.beginTime = 1e-100;
    customFrameAnimation.fillMode = kCAFillModeRemoved;
    customFrameAnimation.timingFunctions = timingFunctions;
    customFrameAnimation.removedOnCompletion = YES;
    [animLayer addAnimation:customFrameAnimation forKey:nil]; 

Thanks in advance.


Solution

  • Your animation will need its calculationMode set to kCAAnimationDiscrete.

    Take a look at the documentation on keyTimes which describes how the calculationMode is used:

    The appropriate values in the keyTimes array are dependent on the calculationMode property.

    • If the calculationMode is set to kCAAnimationLinear, the first value in the array must be 0.0 and the last value must be 1.0. Values are interpolated between the specified key times.

    • If the calculationMode is set to kCAAnimationDiscrete, the first value in the array must be 0.0.

    • If the calculationMode is set to kCAAnimationPaced or kCAAnimationCubicPaced, the keyTimes array is ignored.

    If the values in the keyTimes array are invalid or inappropriate for the calculationMode, the keyTimes array is ignored.

    And then you can read the description of the calculation modes:

    Value calculation modes

    These constants are used by the calculationMode property.

    NSString * const kCAAnimationLinear;

    NSString * const kCAAnimationDiscrete;

    NSString * const kCAAnimationPaced;

    Constants

    kCAAnimationLinear

    Simple linear calculation between keyframe values.
    Available in Mac OS X v10.5 and later. Declared in CAAnimation.h.

    kCAAnimationDiscrete

    Each keyframe value is used in turn, no interpolated values are calculated. Available in Mac OS X v10.5 and later. Declared in CAAnimation.h.

    kCAAnimationPaced

    Keyframe values are interpolated to produce an even pace throughout the animation.
    Available in Mac OS X v10.5 and later. Declared in CAAnimation.h.

    In other words, the discrete calculation mode makes the animation jump to each key frame rather than animate/transition to it.

    Best regards.