Search code examples
cocoacore-animationcalayer

Remove Interpolation of CALayer's contents property


Re-asking the question:

When you add an animation for the contents key, a CATransitionAnimation is apparently being triggered that fades the original contents property to the first value in the animation's values array, resulting in a .25 second fade. And it looks bad! I have suppressed every animatable property using all the methods discussed here (returning null animations through a delegate, into the actions dictionary, using CATransaction), but none of these seem to be targeting this particular transition animation.

alt text

I have been looking into what property could possibly be responsible for this, but cannot figure it out.

I need to suppress the transition animation that is occurring when you add an animation to the contents key.

As I'm at such a loss, I will put the keyframe animation that is being added for you to see. I figure maybe I am doing something wrong here? Just a note, that array is just an array of 6 CGImageRefs (the frames of the animation).

+ (CAKeyframeAnimation *)moveLeftAnimation {

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
animation.values = [NSArray arrayWithArray:[Setzer walkingLeftSprite]];
animation.duration = 0.5f;
animation.keyTimes = [NSArray arrayWithObjects:
                      [NSNumber numberWithFloat:0.0], 
                      [NSNumber numberWithFloat:0.2], 
                      [NSNumber numberWithFloat:0.4],
                      [NSNumber numberWithFloat:0.6],
                      [NSNumber numberWithFloat:0.8],
                      [NSNumber numberWithFloat:1.0],
                      nil];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
return animation;

}

Also, I have an animation that handles the position key, in the sprite's action dictionary:

+ (CABasicAnimation *)moveAnimation {

CABasicAnimation *moveAnimation = [CABasicAnimation animation];
moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
moveAnimation.duration = 0.5f;

return moveAnimation;

}

I am thinking maybe this transition is occurring when you change the layer's position? I don't know...

Please help! This is driving me NUTS!


Solution

  • You can do something along the lines of what I describe in this answer, where I disable the implicit animations for various layer properties by setting the appropriate values in the actions dictionary on that layer.

    In your case, I believe something like

    NSMutableDictionary *newActions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                       [NSNull null], @"contents",
                                       nil];
    layer.actions = newActions;
    [newActions release];
    

    should prevent the implicit animation of a layer's contents property until you explicitly animate it.