removeOnCompletion and challenge


#1

So i did the challenge, and I am trying to chain together the button sliding onto the screen FOLLOWED by the text label spinning. So what I did is I set the delegate for the button sliding on the screen inside of viewDidAppear: to self, and from reading elsewhere I set the removeOnCompletion property of the animation to NO so as I can test for it in the animationDidStop: method because otherwise the animation is destroyed before it actually gets to the animationDidStopMethod:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear];
    CABasicAnimation *showButton = 
        [CABasicAnimation animationWithKeyPath:@"position.x"];
    [showButton setDuration:0.5];
    [showButton setFromValue:[NSNumber numberWithFloat:(160.0+[askTime bounds].size.width)]];
    [showButton setToValue:[NSNumber numberWithFloat:160.0]];
    
    CAMediaTimingFunction *tf = [CAMediaTimingFunction
                                 functionWithName:kCAMediaTimingFunctionEaseOut];
    [showButton setTimingFunction:tf];
    
    [showButton setDelegate:self];

    showButton.removedOnCompletion = NO;   // So what happens to the memory that the show button has?
    
    [[askTime layer] addAnimation:showButton      // askTime is my property name for the button
                           forKey:@"slideButton"];
    
    
}

and then in animationDidStop I triggered the spinning (showCurrentTime:) if I have the correct animator:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (anim == [[askTime layer] animationForKey:@"slideButton"]) {  // this only worked when I had
                                                                                                    //     showButton.removedOnCompletion = NO;
                                                                                                    // in above code
        [self showCurrentTime:nil];
    }
    
    NSLog(@"%@ finished: %d", anim, flag);
}

Now I want to log each time my spinning animation ends, so it has the delegate set as well.
Now ideally, I could use this for test for all sort of animation completion events. But what I am confused about is:

  1. If I set the removedOnCompletion flag to NO for each animation I create and add, don’t I end up with memory leaks?
  2. Is there a way to remove them after, but not have the app crash? (I tried [[[askTime layer] animationForKey:@“slideButton”] release], but then it crashes when it does the check the next time. Surprisingly, if I don’t set the removedOnCompletion flag to NO, the check fails but the application does not crash)
  3. If not, is there a way for me to set a specific selector for each animation when it ends? I see I can do something like that for views when I do animation blocks with beginAnimation and endAnimation, but is there a way to do that with layers and simple animations?

Overall, I am confused by the purpose of that key and whether any significant memory is retained by the animation. What is the purpose of the key in addAnimation:forKey?