CA crossfade

  • 10.7 SDK, 10.7 target. The content view of the window is layer-hosting, containing only a CALayer.

    I'm trying to crossfade the contents image of the CALayer from Image A (the existing contents) to Image B (the new contents). Surely this is simple. I'm thrashing.

    Here is an excerpt from my controller object's set-image method. I've seen the following code (more or less) in responses on the Web, and it has always met with apparent satisfaction, but it does not work as I intend: Image A is instantly replaced by Image B, then the crossfade from A to B runs. Image B remains.

    =====
    //  Initialize nsImage with the new contents.
    CABasicAnimation *    crossfade;
    crossfade = [CABasicAnimation animationWithKeyPath: @"contents"];

    crossfade.duration = 0.75;
    crossfade.removedOnCompletion = YES;
    crossfade.fromValue = self.backgroundLayer.contents;
    crossfade.toValue = nsImage;
    [self.backgroundLayer addAnimation: crossfade forKey: nil];

    self.backgroundLayer.contents = nsImage;
    =====

    Commenting-out the assignments to .fromValue and .toValue yields the same effect.

    Okay, try this: add

    crossfade.delegate = self;

    before the addAnimation:, and delete

    self.backgroundLayer.contents = nsImage;

    Add the delegate method:

    - (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
    {
        NSImage *  newImage = [(CABasicAnimation *)anim toValue];
        self.backgroundLayer.contents = newImage;
    }

    Printing the nsImage pointer in the set-image method and newImage in the animationDidStop: method shows that the two pointers are equal.

    In this case, the animation runs from Image A to Image B, then jumps to Image A, then jumps to Image B.

    In all cases, self.backgroundLayer.actions is nil.

    I'm thrashing at a task that ought to be simple and obvious. What am I missing?

    — F
  • I'm an adventurer, so I tried this:

    =======
    self.backgroundLayer.contents = nsImage;

    CABasicAnimation *    crossfade;
    crossfade = [CABasicAnimation animationWithKeyPath: @"contents"];
    crossfade.duration = 2.0;
    crossfade.removedOnCompletion = YES;
    [self.backgroundLayer addAnimation: crossfade forKey: nil];
    =======

    This works as I expect: The contents of the layer transition smoothly, without flicker.

    — F

    On 15 Jul 2012, at 4:08 PM, Fritz Anderson wrote:

    > 10.7 SDK, 10.7 target. The content view of the window is layer-hosting, containing only a CALayer.
    >
    > I'm trying to crossfade the contents image of the CALayer from Image A (the existing contents) to Image B (the new contents). Surely this is simple. I'm thrashing.
    >
    > Here is an excerpt from my controller object's set-image method. I've seen the following code (more or less) in responses on the Web, and it has always met with apparent satisfaction, but it does not work as I intend: Image A is instantly replaced by Image B, then the crossfade from A to B runs. Image B remains.
    >
    > =====
    > //  Initialize nsImage with the new contents.
    > CABasicAnimation *    crossfade;
    > crossfade = [CABasicAnimation animationWithKeyPath: @"contents"];
    >
    > crossfade.duration = 0.75;
    > crossfade.removedOnCompletion = YES;
    > crossfade.fromValue = self.backgroundLayer.contents;
    > crossfade.toValue = nsImage;
    > [self.backgroundLayer addAnimation: crossfade forKey: nil];
    >
    > self.backgroundLayer.contents = nsImage;
    > =====
    >
    > Commenting-out the assignments to .fromValue and .toValue yields the same effect.
    >
    > Okay, try this: add
    >
    > crossfade.delegate = self;
    >
    > before the addAnimation:, and delete
    >
    > self.backgroundLayer.contents = nsImage;
    >
    > Add the delegate method:
    >
    > - (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
    > {
    > NSImage *  newImage = [(CABasicAnimation *)anim toValue];
    > self.backgroundLayer.contents = newImage;
    > }
    >
    > Printing the nsImage pointer in the set-image method and newImage in the animationDidStop: method shows that the two pointers are equal.
    >
    > In this case, the animation runs from Image A to Image B, then jumps to Image A, then jumps to Image B.
    >
    > In all cases, self.backgroundLayer.actions is nil.
    >
    > I'm thrashing at a task that ought to be simple and obvious. What am I missing?
    >
    > — F
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today