UIView animation end

2019-05-28 12:17发布

问题:

I am using UIView animation in my app:

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        //do some animation
        [UIView commitAnimations];

Now, there is a possibility that the animation would take a couple of seconds to finish. So, is there a way to know when the animation has ended?

回答1:

I haven't uses this myself yet, but there is a method setAnimationDidStopSelector that allows you to define a selector that should be called when an animation has finished.

Also see the documentation at Apple for UIView.

There is also an example on Ray Wenderlich's site



回答2:

Use the latest block-based animation methods provided by apple as:

    [UIView animateWithDuration:0.5 
                      delay:0.0 
                    options:UIViewAnimationOptionBeginFromCurrentState 
                 animations:^{
                     //animation block
                 }
                 completion:^(BOOL finished){//this block starts only when 
                 //the animation in the upper block ends
                 //so you know when exactly the animation ends
     }];

The method you have used will be deprecated soon...