Resetting of property values after completion of a CABasicAnimation

  • Dear List,

    I am using a CABasicAnimation to fade an item out. I am setting the
    opacity to 1.0 in the fromValue and to 0.0 in the toValue.The
    animation works perfectly and the item fades out, however once the
    animation has completed, the property (in this case opacity) seems to
    get reset to the original value of 1.0 so my item sudden pops back
    before I get a chance to remove it completely. I have tried everything
    I can think of to stop this irritating flash to no avail. I list my
    code below. Any suggestions / criticisms would be very welcome.

    TIA

    Vincent

    - (void) fadeOut
    {
    CABasicAnimation *theAnimation;
    ItemAnimationContext* context;

    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    theAnimation.duration=1.0;
    theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
    theAnimation.toValue=[NSNumber numberWithFloat:0.0];
    context = [ItemAnimationContext onAnimation: theAnimation endAction:
    @selector(fadeOutCompleted:) onItem: self];
    [theAnimation setDelegate: context];
    [[self layer] addAnimation:theAnimation forKey:@"animateOpacity"];
    }
  • Hi Vincent,

    I have a couple of ideas;

    - set the removedOnCompletion property to NO on your basic animation
    (i think the flash is happening because by default it is removed and
    since you have not set the alpha to zero it bounces back to 1)

    - use implicit animation instead of explicit and just tell the layer
    to set its opacity to zero (which will animate automatically) and if
    you need the duration to be one second you can specify that in a
    transaction (check out http://developer.apple.com/documentation/Cocoa/Conceptual/CoreAnimation_gui
    de/Articles/Transactions.html
    )

    - use a transition animation and remove the layer from its superlayer,
    set the type to kCATransitionFade and the layer will fade out before
    its removed

    HTH,

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

    On Nov 13, 2007, at 4:13 AM, Vincent Coetzee wrote:

    > Dear List,
    >
    > I am using a CABasicAnimation to fade an item out. I am setting the
    > opacity to 1.0 in the fromValue and to 0.0 in the toValue.The
    > animation works perfectly and the item fades out, however once the
    > animation has completed, the property (in this case opacity) seems
    > to get reset to the original value of 1.0 so my item sudden pops
    > back before I get a chance to remove it completely. I have tried
    > everything I can think of to stop this irritating flash to no avail.
    > I list my code below. Any suggestions / criticisms would be very
    > welcome.
    >
    > TIA
    >
    > Vincent
    >
    > - (void) fadeOut
    > {
    > CABasicAnimation *theAnimation;
    > ItemAnimationContext*    context;
    >
    > theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    > theAnimation.duration=1.0;
    > theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
    > theAnimation.toValue=[NSNumber numberWithFloat:0.0];
    > context = [ItemAnimationContext onAnimation: theAnimation
    > endAction: @selector(fadeOutCompleted:) onItem: self];
    > [theAnimation setDelegate: context];
    > [[self layer] addAnimation:theAnimation forKey:@"animateOpacity"];
    > }
  • Hi Bill,

    Thanks very much for the suggestion, the removeOnCompletion property
    did not make any difference, the flash still occurred, however your
    second suggestion, using transactions worked like a charm. Problem
    solved.

    Thanks

    Vincent

    On 13 Nov 2007, at 1:27 PM, Bill Dudney wrote:

    > Hi Vincent,
    >
    > I have a couple of ideas;
    >
    > - set the removedOnCompletion property to NO on your basic animation
    > (i think the flash is happening because by default it is removed and
    > since you have not set the alpha to zero it bounces back to 1)
    >
    > - use implicit animation instead of explicit and just tell the layer
    > to set its opacity to zero (which will animate automatically) and if
    > you need the duration to be one second you can specify that in a
    > transaction (check out http://developer.apple.com/documentation/Cocoa/Conceptual/CoreAnimation_gui
    de/Articles/Transactions.html
    )
    >
    > - use a transition animation and remove the layer from its
    > superlayer, set the type to kCATransitionFade and the layer will
    > fade out before its removed
    >
    > HTH,
    >
    >
    > -bd-
    > http://bill.dudney.net/roller/objc
    >
    > On Nov 13, 2007, at 4:13 AM, Vincent Coetzee wrote:
    >
    >> Dear List,
    >>
    >> I am using a CABasicAnimation to fade an item out. I am setting the
    >> opacity to 1.0 in the fromValue and to 0.0 in the toValue.The
    >> animation works perfectly and the item fades out, however once the
    >> animation has completed, the property (in this case opacity) seems
    >> to get reset to the original value of 1.0 so my item sudden pops
    >> back before I get a chance to remove it completely. I have tried
    >> everything I can think of to stop this irritating flash to no
    >> avail. I list my code below. Any suggestions / criticisms would be
    >> very welcome.
    >>
    >> TIA
    >>
    >> Vincent
    >>
    >> - (void) fadeOut
    >> {
    >> CABasicAnimation *theAnimation;
    >> ItemAnimationContext*    context;
    >>
    >> theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    >> theAnimation.duration=1.0;
    >> theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
    >> theAnimation.toValue=[NSNumber numberWithFloat:0.0];
    >> context = [ItemAnimationContext onAnimation: theAnimation
    >> endAction: @selector(fadeOutCompleted:) onItem: self];
    >> [theAnimation setDelegate: context];
    >> [[self layer] addAnimation:theAnimation forKey:@"animateOpacity"];
    >> }
    >
  • On Nov 13, 2007, at 9:57 AM, <cocoa-dev-request...> wrote:

    > second suggestion, using transactions worked like a charm. Problem
    > solved.
    >

    Dear Bill & Vincent (and everyone else),

    While I am glad your code is doing what you want, your original
    question never was answered. I am having similar problem, or at least,
    I am having hard time understanding what is going on when using
    CABasicAnimation.

    All the sample documentation and examples use CABasicAnimation to
    create an explicit animation from some property value (using changing
    Opacity) to another value and then back again. They do not show you
    how to explicitly animate a property value so that the value stays
    changed.

    I want to animate from value (in my case, the layer's position), and
    have the value be valid at the end of the animation. When I use
    CABasicAnimation animation to do this, the position of the layer
    changes (a nice smooth animation), but then the layer jumps back to
    it's original position.

    Yes, I could use implicit animation to do similar effect, but I am
    going to be grouping animations together, with different start times,
    so I need to make it an explicit animation.

    Can anyone explain what I am doing wrong?

    Steve Sheets
    Midnight Mage Software
  • Hi Steve,

    Keep in mind that animation (by its self) is not changing the
    underlying property. When you attach an animation to an object it will
    make the animation happen but its up to you to make the change to the
    'real' object. For example if you create a basic animation that moves
    the frame.origin.x from 14 to 25 (i.e the fromValue is {14.0f, 35.0f}
    and the toValue is {15.0f, 35.0f}) but the view is actually at {35.0f,
    35.0f} and you set the new location to {19.0f, 35.0f} the animation
    will move from 14 to 25. So the result will be flashing and popping
    around. Initially the view will appear to jump to 14 (from 35) then
    will smoothly animate to 25 then will jump to 19.

    All that said I posted this;

    http://bill.dudney.net/roller/objc/resource/TimedAnimaton.zip

    example. I'll write up a blog post about it RSN but in the mean time,
    run and go then click the right arrow key. The beach photo moves to
    the snow photo then when its under the snow photo the snow photo
    starts  its animation and then moves to the far left.

    Its not beautiful but I think it shows at least a way to do what you
    are looking to do.

    HTH,

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

    On Nov 14, 2007, at 8:08 AM, Steve Sheets wrote:

    >
    > On Nov 13, 2007, at 9:57 AM, <cocoa-dev-request...> wrote:
    >
    >> second suggestion, using transactions worked like a charm. Problem
    >> solved.
    >>
    >
    > Dear Bill & Vincent (and everyone else),
    >
    > While I am glad your code is doing what you want, your original
    > question never was answered. I am having similar problem, or at
    > least, I am having hard time understanding what is going on when
    > using CABasicAnimation.
    >
    > All the sample documentation and examples use CABasicAnimation to
    > create an explicit animation from some property value (using
    > changing Opacity) to another value and then back again. They do not
    > show you how to explicitly animate a property value so that the
    > value stays changed.
    >
    > I want to animate from value (in my case, the layer's position), and
    > have the value be valid at the end of the animation. When I use
    > CABasicAnimation animation to do this, the position of the layer
    > changes (a nice smooth animation), but then the layer jumps back to
    > it's original position.
    >
    > Yes, I could use implicit animation to do similar effect, but I am
    > going to be grouping animations together, with different start
    > times, so I need to make it an explicit animation.
    >
    > Can anyone explain what I am doing wrong?
    >
    > Steve Sheets
    > Midnight Mage Software
  • >
    > Keep in mind that animation (by its self) is not changing the
    > underlying property.

    This is misleading. The animation does change the property but the way
    it figures out what to set it to is based on interpolating between the
    from and to values. So in the first 'time slot' the view will jump to
    where the interpolation calculations direct. If that is not 'smooth'
    then you get the jumping.

    Sorry for the confusion.

    TTFN,

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

    >
    >
    > On Nov 14, 2007, at 8:08 AM, Steve Sheets wrote:
    >
    >>
    >> On Nov 13, 2007, at 9:57 AM, <cocoa-dev-request...> wrote:
    >>
    >>> second suggestion, using transactions worked like a charm.
    >>> Problem  solved.
    >>>
    >>
    >> Dear Bill & Vincent (and everyone else),
    >>
    >> While I am glad your code is doing what you want, your original
    >> question never was answered. I am having similar problem, or at
    >> least, I am having hard time understanding what is going on when
    >> using CABasicAnimation.
    >>
    >> All the sample documentation and examples use CABasicAnimation to
    >> create an explicit animation from some property value (using
    >> changing Opacity) to another value and then back again. They do not
    >> show you how to explicitly animate a property value so that the
    >> value stays changed.
    >>
    >> I want to animate from value (in my case, the layer's position),
    >> and have the value be valid at the end of the animation. When I use
    >> CABasicAnimation animation to do this, the position of the layer
    >> changes (a nice smooth animation), but then the layer jumps back to
    >> it's original position.
    >>
    >> Yes, I could use implicit animation to do similar effect, but I am
    >> going to be grouping animations together, with different start
    >> times, so I need to make it an explicit animation.
    >>
    >> Can anyone explain what I am doing wrong?
    >>
    >> Steve Sheets
    >> Midnight Mage Software

previous month november 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    
Go to today