UIView animation completion callback?

Slee picture Slee · Jul 2, 2010 · Viewed 14.5k times · Source

Can I setup a function to be called once my animation is complete? I want to fade a UIView and then remove it from the superView.

Answer

SK9 picture SK9 · Jun 2, 2011

Animation blocks were introduced in iOS4. Apple recommends you use these, and the new methods mostly ask for completion blocks which replace callbacks. For example:

[UIView animateWithDuration:0.5f
                      delay:0.0f
                    options:UIViewAnimationCurveEaseInOut
                 animations:^{
                   [myView setAlpha:0.0f];
                 }
                 completion:^(BOOL finished) {
                   [myView removeFromSuperview];
                 }];