Le Mot Juiced
Le Mot Juiced

Reputation: 3859

dismissViewControllerAnimated: completion: happens instantly unless I kludge it

I have a custom UIViewController that's being displayed modally. I'm calling it mainMenu.

It has its own cute little transition animation it does to make its views slide off screen. When I want to dismiss it, I want to call the custom animation and then actually dismiss it once that's done. It seems like this should work:

- (void) dismissCustomViewController {
    [mainMenu slideMenuPanelsAway];
    [self dismissViewControllerAnimated:YES completion:nil];
}

However, this makes the view controller vanish instantly, before I get to see the custom slidey stuff.

What's the right way to make the view controller wait until the menus are gone before vanishing?

I've tried a bunch of things. I only found one way to make it work:

- (void) dismissCustomViewController {
    [mainMenu slideMenuPanelsAway];
    [self performSelector:@selector(dismissController) withObject:nil 
       afterDelay: 2.0f];
}

(I wrote a custom method called dismissController just to make the selector easier to use, it basically just calls [self dismissViewControllerAnimated:YES completion:nil];.)

It just seems awful kludgey to use a manual delay setting instead of actually basing it on the completion of the animation. There's got to be a better way, doesn't there?

Upvotes: 0

Views: 476

Answers (1)

rdelmar
rdelmar

Reputation: 104092

Use animateWithDuration:animations:completion:, and do the "slidey stuff" in the animation block and do the dismissal in the completion block.

[UIView animateWithDuration:.5 animations:^{
        //Your custom animation stuff here

    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];

Upvotes: 3

Related Questions