Core animation update layer properties

  • Hey there,

    I want to fade out a layer (from opacity 1 to 0) using a
    CABasicAnimation.

    I have tried the following:

    CABasicAnimation *a = [CABasicAnimation
    animationWithKeyPath:@"opacity"];
    a.fromValue = [NSNumber numberWithFloat:1.0];
    a.toValue = [NSNumber numberWithFloat:0.0];
    a.duration = 1.0;
    [layer addAnimation:a forKey:@"myFadeOut"];

    This works fine, however when the animation completes the layers
    opacity reverts back to its value before the animation began (1.0 in
    this case).

    Is there any way I can tell the animation to update the real opacity
    property, instead of just the 'presentation' one?
  • Hi,

    I would approach it in one of these ways;

    1) Remove the layer from its superlayer using a fade transition
    2) Call layer.opacity = 0.0f with the timing set to one second on a
    transaction

    HTH,

    -bd-
    http://bill.dudney.net/roller/objc

    On Dec 9, 2007, at 7:57 AM, Nik Youdale wrote:

    > Hey there,
    >
    > I want to fade out a layer (from opacity 1 to 0) using a
    > CABasicAnimation.
    >
    > I have tried the following:
    >
    > CABasicAnimation *a = [CABasicAnimation
    > animationWithKeyPath:@"opacity"];
    > a.fromValue = [NSNumber numberWithFloat:1.0];
    > a.toValue = [NSNumber numberWithFloat:0.0];
    > a.duration = 1.0;
    > [layer addAnimation:a forKey:@"myFadeOut"];
    >
    > This works fine, however when the animation completes the layers
    > opacity reverts back to its value before the animation began (1.0 in
    > this case).
    >
    > Is there any way I can tell the animation to update the real opacity
    > property, instead of just the 'presentation' one?
  • On Dec 9, 2007, at 6:57 AM, Nik Youdale wrote:

    > Hey there,
    >
    > I want to fade out a layer (from opacity 1 to 0) using a
    > CABasicAnimation.
    >
    > I have tried the following:
    >
    > CABasicAnimation *a = [CABasicAnimation
    > animationWithKeyPath:@"opacity"];
    > a.fromValue = [NSNumber numberWithFloat:1.0];
    > a.toValue = [NSNumber numberWithFloat:0.0];
    > a.duration = 1.0;
    > [layer addAnimation:a forKey:@"myFadeOut"];
    >
    > This works fine, however when the animation completes the layers
    > opacity reverts back to its value before the animation began (1.0 in
    > this case).
    >
    > Is there any way I can tell the animation to update the real opacity
    > property, instead of just the 'presentation' one?

    If you want the animation to stick after the fact, then you need to do
    two things.
    1) Set the fillMode property to kCAFillModeForwards (this sets the
    animation to "stick" after its duration)
    2) Set the removedOnCompletion property to NO (or the animation will
    be removed when its duration completes and it will rebound anyway).

    You will likely also want to set a delegate on the animation so that
    you know when it completes to do whatever additional actions are
    necessary afterwards (such as removing the layer from the layer tree).
    --
    David Duncan
    Apple DTS Animation and Printing
    <david.duncan...>
previous month december 2007 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