Search code examples
iphonecore-animation

"Bending" a UIView in 3D


What would be the best way of creating a CA animation that gives a sense of curvature to the view?

My initial approach is to create a grid of CALayers, each with a unique transformation matrix (I have some experience with OpenGL and this is how I would to this in GL). To do this I would need to divide the view in several quads (each represented by a CALayer). The question is, how can I do this. It would be allright to just take a snapshot of the view at animation start and use until the animation is complete (like transitions can do), but I am not sure how to take such a snapshot.

Any pointers (especially with links to code examples) would be of golden value :)


Solution

  • Classic answer:

    I think what you're probably looking for is CALayer's renderInContext:. Every UIView has a CALayer, which you can get via the layer property. So you can build the CGContext you want (which will be a bitmap context, presumably), have the view's layer draw the view to the context, then do whatever you want with that data, whether it's divide it up into a bunch of UIImages and hence UIImageViews for a CoreAnimation effect, upload it to OpenGL or whatever.

    Example code is provided directly by Apple in Q&A 1703, which uses the UIKit CoreGraphics extensions to jump straight to a UIImage and digs through all the available windows to capture an image of the entire screen. But it shows you the essence of the thing.


    Updated in 2015:

    As of iOS 7, as documented in Q&A 1817, UIView provides -drawViewHierarchyInRect:afterScreenUpdates: which reduces the code necessary to snapshot a view even further — to four lines in Apple's example code. It appears Q&A 1703 has been withdrawn as it is no longer available on Apple's side; the link above is to an external archived copy. So the 1817 advice is the stuff to follow.