Chris C
Chris C

Reputation: 3251

How to create a custom UIViewController transition animation?

This question seems to get asked a lot but I have never found a definitive answer as to whether or not it's possible to have custom transitions the same way UIKit does.

I know you can do tricks like take a screenshot of the current view and the upcoming view, and then animate those while you change view controllers under the animation. However, this takes quite a bit of memory, as you have basically 2 full extra screens worth of drawing (because of the screenshots).

I'm looking for a more elegant way of presenting view controllers with a custom animation. Or, is there a more memory-efficient way of doing the above approach?

Upvotes: 2

Views: 1389

Answers (2)

rdelmar
rdelmar

Reputation: 104092

There are several ways to accomplish this, depending on how you want to transition, whether your controllers are embedded in a container controller, etc. In the simplest case, where you have a single controller, and you want to transition to another controller, you can do it with the following steps:

  1. instantiate the new controller (lets call it B, and the original one A)
  2. add B's view as a subview of the window (gotten from self.view.window)
  3. set B's frame to be off screen in which ever direction you want or make its alpha 0, or make it have zero size, depending on what kind of transition you want.
  4. do what ever transition you want with animateWithDuration:animations:completion: remove A's view (in the completion block)
  5. in the completion block, make B the root view controller of the window

Upvotes: 5

Stephen J
Stephen J

Reputation: 2397

Essentially you're having the system automatically retain and release, along with do whatever is happening below the calls rdelmar's answer outlined. Transitioning a view controller is just animating a view while having the system keep your controller in memory.

But, to answer more of your questions, UIView animations create duplicates and cache the images, too. (Btw, the system cache doesn't unload its memory, so avoid UIImage imageNamed.

You will probably want to code it yourself using a timer if you want efficiency, and drum up all the graphics tricks you can think of.

And never, ever trust iOS to work the same way it did the last version. Everything I said is subject to being wrong the same way iOS 6 broke retain on annotating, and the manuals stated iOS 3 animations will become obsolete (and are now replaced instead?) gah, just saying be careful with efficiency, it may be broken or updated in the somewhat distant future if you try it.

Upvotes: 1

Related Questions