-3

I have few subviews responding to the same animation. I want remove the the subview after the animations has finish (animationDidStop ). Is a way to detect what subview was animated in animationDidStop ?

in this particular case:

 if ([animationID isEqualToString:@"myAnimation"])
    view.removeSubView:myView;

how do you if is the correct subview?, because I as I said before some of the subviews respond to the same animation.

Juan
  • 627
  • 2
  • 9
  • 27

1 Answers1

3

You can name your animations (each view will require a unique animation name):

[UIView beginAnimations:@"myAnimation" context:nil];
myView.alpha = 1.0f;     // or whatever property you're animating...
[UIView commitAnimations];

and then retrieve this name in

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(id)context
{
    if ([finished boolValue])
    {
        if ([animationID isEqualToString:@"myAnimation"])
            [myView removeFromSuperview];
    }
}

or better yet, use the iOS4+ block animation method and its completion handler, you'll be able to much more cleanly reference the view being animated right in the completion handler. e.g.

[UIView animateWithDuration:0.4f animations:^
{ 
    myView.alpha=1.0f; 
} 
completion:^(BOOL finished)
{
    [myView removeFromSuperview];
}];
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
CSmith
  • 13,318
  • 3
  • 39
  • 42