jfisk
jfisk

Reputation: 6205

What's the proper way to implement complex custom view controllers

Note: I'm not talking about custom view controller transition effects which can be done by using a custom view controllers it's the iOS 5+ API.

I'm talking about transitioning to another view controller, where a view from the presently displayed view controller is animated to the view controller to be presented's view.

EXAMPLE

-you have friendsViewController which displays a list of the current users friends. Each table view cell has a profile picture and name.

-click on a cell, all other cells fade away and the name and picture animate to the top. At this point, UserProfileViewComtroller is displayed.

THEORIES

-I could easily do this by combining the two view controllers, but UserProfileViewComtroller can be launched from other parts of the app.

-if the UserProfileViewControllers view is instantiated, I could convert the coordinates using UIViews methods

I feel like there is a more appropriate/cleaner solution here which is why I'm asking the community for help :)

Upvotes: 0

Views: 188

Answers (2)

ilya n.
ilya n.

Reputation: 18816

It seems to me that what you want is exactly about view controllers transition, since you want to do 'something' that would look to the user as if you took a view from old VC and moved it to the new VC.

Then you're in luck, as you're allowed to move a UIView from one view controller to another using [superview addSubview:view] as part of the transition you want to do.

This can be done on any iOS version, although it's easier now as in iOS 7 there's a delegate you write (see <UIViewControllerAnimatedTransitioning> reference) which has access to both VC's view hierarchies and can change them at will (move one view, fade other views) during transition period.

Also, making your new view controller during the transition transparent (or using old controller's snapshot) will help you hide the fact that VC changed.

Upvotes: 2

David Hoerl
David Hoerl

Reputation: 41622

Not so much an answer but a technique that might inspire a solution. I did an app that had need for a custom transition like this. The original app arranged itself then took a snapshot, so at the last moment the user is looking at an image. The second viewController was created, given coordinates etc, and the image, then shown immediately. It put the image into its view (subview with same bounds).

At this point the second vc has complete control, and can fade in some other content etc. the reverse was more or less as the start - the image is used, swapped, used removed to uncover the real view content.

Note that this took a bit of time to get it working with no glitches etc.

EDIT: if you are concerned in turning the whole original view into an image, then modify the technique. For instance, in the original view, fade all other content to black but the cell, then snapshot the one cell. The second view will start with an all black background, and place the cell image over top it, then go from there.

EDIT2: As mentioned in the comments, you of course push the second view with no animation, so it happens instantaneously. By setting a small image on the second vc, with an agreed upon background, you can quickly "pass the baton" so to speak and let the second controller go to work quickly and seamlessly.

Upvotes: 0

Related Questions