Removing CALayer after Animation

  • Another question on Core Animation.

    I am creating temporary CALayers, and animating them across a parent layer.
    I would like to delete the layers once they have reached their destination.
    I are using the delegate method animationDidStop:finished: on the animation,
    but it returns only a reference to the animation object that finished, and I
    don't know how to figure out what layer the animation was associated.
    Without this information, I don't know which layer we should remove. Any
    help would be great.

    Thank You,
    Bridger Maxwell
  • Bridger,

    As far as I can tell there is nothing inherent in the CA API to do
    what you want, however, KVC is available to you. When you create your
    animation, do this:

    [animation setValue:objectLayer forKey:@"parentLayer"];

    Where objectLayer is the layer the animation is going to be run on.
    Then, in your -animationDidStop, you can grab the layer like this:

    - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
    {
      CALayer *layer = [theAnimation valueForKey:@"parentLayer"];
      if( layer )
      {
        NSLog(@"The layer object was: %@ (%@)", layer, [layer name]);
        [layer removeFromSuperlayer];
      }
    }

    I named my layer when I created it with a call to [objectLayer
    setName:@"parentLayer"] in this code, so the NSLog in -
    animationDidStop won't display the name unless you've done the same.

    Best regards,

    -Matt

    On Jan 3, 2009, at 9:47 PM, Bridger Maxwell wrote:

    > Another question on Core Animation.
    >
    > I am creating temporary CALayers, and animating them across a parent
    > layer.
    > I would like to delete the layers once they have reached their
    > destination.
    > I are using the delegate method animationDidStop:finished: on the
    > animation,
    > but it returns only a reference to the animation object that
    > finished, and I
    > don't know how to figure out what layer the animation was associated.
    > Without this information, I don't know which layer we should remove.
    > Any
    > help would be great.
    >
    > Thank You,
    > Bridger Maxwell
  • Hey,
    Is the animation your own subclass of CAAnimation? I am justing using
    CAKeyframeAnimation.

    TTFN
    Bridger

    On Tue, Jan 6, 2009 at 1:45 PM, Matt Long <matt.long...>wrote:

    > Bridger,
    >
    > As far as I can tell there is nothing inherent in the CA API to do what you
    > want, however, KVC is available to you. When you create your animation, do
    > this:
    >
    > [animation setValue:objectLayer forKey:@"parentLayer"];
    >
    > Where objectLayer is the layer the animation is going to be run on. Then,
    > in your -animationDidStop, you can grab the layer like this:
    >
    > - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
    > {
    > CALayer *layer = [theAnimation valueForKey:@"parentLayer"];
    > if( layer )
    > {
    > NSLog(@"The layer object was: %@ (%@)", layer, [layer name]);
    > [layer removeFromSuperlayer];
    > }
    > }
    >
    > I named my layer when I created it with a call to [objectLayer setName:@"parentLayer"]
    > in this code, so the NSLog in -animationDidStop won't display the name
    > unless you've done the same.
    >
    > Best regards,
    >
    > -Matt
    >
    >
    >
    > On Jan 3, 2009, at 9:47 PM, Bridger Maxwell wrote:
    >
    > Another question on Core Animation.
    >>
    >> I am creating temporary CALayers, and animating them across a parent
    >> layer.
    >> I would like to delete the layers once they have reached their
    >> destination.
    >> I are using the delegate method animationDidStop:finished: on the
    >> animation,
    >> but it returns only a reference to the animation object that finished, and
    >> I
    >> don't know how to figure out what layer the animation was associated.
    >> Without this information, I don't know which layer we should remove. Any
    >> help would be great.
    >>
    >> Thank You,
    >> Bridger Maxwell
    >>
    >
  • No. It is a CABasicAnimation, but it also works with a
    CAKeyframeAnimation.

    I modified the example project from this blog post: http://www.cimgf.com/2008/11/05/core-animation-tutorial-interrupting-animat
    ion-progress/

      to demonstrate this. The modified project is here: http://www.cimgf.com/files/FollowMe.zip
      .

    The sample code does not actually call -removeFromSuperlayer as the
    layer gets referenced again which would cause it to crash. Make sure
    you're not accessing the layer again after you've removed it from it's
    parent. Unless you've explicitly retained it, it's been autoreleased
    at that point.

    hth,

    -Matt

    On Jan 8, 2009, at 12:10 AM, Bridger Maxwell wrote:

    > Hey,
    > Is the animation your own subclass of CAAnimation? I am justing using
    > CAKeyframeAnimation.
    >
    > TTFN
    > Bridger
    >
    > On Tue, Jan 6, 2009 at 1:45 PM, Matt Long <matt.long...>-
    > long.com>wrote:
    >
    >> Bridger,
    >>
    >> As far as I can tell there is nothing inherent in the CA API to do
    >> what you
    >> want, however, KVC is available to you. When you create your
    >> animation, do
    >> this:
    >>
    >> [animation setValue:objectLayer forKey:@"parentLayer"];
    >>
    >> Where objectLayer is the layer the animation is going to be run on.
    >> Then,
    >> in your -animationDidStop, you can grab the layer like this:
    >>
    >> - (void)animationDidStop:(CAAnimation *)theAnimation finished:
    >> (BOOL)flag
    >> {
    >> CALayer *layer = [theAnimation valueForKey:@"parentLayer"];
    >> if( layer )
    >> {
    >> NSLog(@"The layer object was: %@ (%@)", layer, [layer name]);
    >> [layer removeFromSuperlayer];
    >> }
    >> }
    >>
    >> I named my layer when I created it with a call to [objectLayer
    >> setName:@"parentLayer"]
    >> in this code, so the NSLog in -animationDidStop won't display the
    >> name
    >> unless you've done the same.
    >>
    >> Best regards,
    >>
    >> -Matt
    >>
    >>