FishStix FishStix - 4 months ago 39
Objective-C Question

Animated Transition between two Modal ViewControllers

I'm trying to switch between two different active Modal View Controllers and trying for a smooth animation. Ideally I would like to have the animation look just like the new Modal View is becoming a Modal View over the current Modal View.

For consistency across my application, I need to limit only one layer of Modal View Controllers displayed at any given time.

Currently I just have the existing Modal View dismiss with no animation and then animate the next Modal View over the RootController, but this doesn't look nice.

Thanks

Answer

You can just present the 2nd model view controller over the first using the default transition. It sounds like your second model view is being allocated and initialized in the same view controller as your first model view. If this is the case, consider refactoring your code so that you would have the first model view controller present the second modal view controller. Doing this would display one over the other like you want.

If you need to keep the code for presentation of both modal view controllers in the same root view controller, you may want to create a delegate method. This would send a message from the first modal view controller back to the root view controller that presented it, passing along a reference to the first modal view controller. Next, use this reference to tell the first modal view controller to present the 2nd modal view controller over it.

I'd definitely recommend the former solution though as it's logically clearer with less chance of introducing a retain cycle.

In response to your clarification:

To transition between two, try:

In your ModalViewControllerOne instance that is already displayed from a previous session:

self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self dismissModalViewControllerAnimated:YES];

In your rootViewController's -viewDidAppear:

ModalViewControllerTwo *modalViewControllerTwo = [[ModalViewControllerTwo alloc] init];
modalViewControllerTwo.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[rootViewController presentModalViewController:modalViewControllerTwo animated:YES];

The idea of this is to cross-dissolve the first modal view controller back to the root view controller, then immediately cross-dissolve the new modal view controller back onto the screen.

If it's just the view that is different between the splash screens, you could instead of two view controllers, have one view controller with logic in that simply swaps out one view for the other depending on the URL entered and use an animation when swapping between the views.

Comments