views:

22

answers:

2

I have an animation that works perfectly on the first invocation. But if I want to animate the very same layer again, using the same code, it completes immediately and the animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag is invoked on the delegate with the flag value NO.

Here is the code that adds the animation:

  imageView.hidden = NO;

  CAKeyframeAnimation* animationOpacity = 
    [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
  ...

  animationOpacity.duration = 2.0;
  animationOpacity.removedOnCompletion = YES;
  animationOpacity.delegate = self;

  [imageView.layer addAnimation:animationOpacity forKey:@"someKey"]; 

and this is the delegate action:

-(void) animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
   imageView.hidden = YES;
}

BTW, Initially the imageView is visible in the XIB.

A: 

Are you adding the animation again before calling it a second time? If you aren't then you should set removedOnCompletion = NO

animationOpacity.removedOnCompletion = NO;
Alan Rogers
Code behaves the same with removedOncompletion `YES` or `NO`. I am adding the animation again, exactly the same code is run, including the `addAnimation:...`
A: 

Turns out to be a combination of setting view.hidden = YES in the callback and calling the animation code from the parent's viewWillApear. Once I moved the animation code call into parent's viewDidApear instead, things started behaving as expected.