views:

7781

answers:

3

I'm looking to perform a perspective transform on a UIView (such as seen in coverflow)

Does anyonew know if this is possible?

I've investigated using CALayer and have run through all the pragmatic programmer Core Animation podcasts, but I'm still no clearer on how to create this kind of transform on an iPhone.

Any help, pointers or example code snippets would be really appreciated!

Cheers, Nick.

+1  A: 

You can only use Core Graphics (Quartz, 2D only) transforms directly applied to a UIView's transform property. To get the effects in coverflow, you'll have to use CATransform3D, which are applied in 3-D space, and so can give you the perspective view you want. You can only apply CATransform3Ds to layers, not views, so you're going to have to switch to layers for this.

Check out the "CovertFlow" sample that comes with Xcode. It's mac-only (ie not for iPhone), but a lot of the concepts transfer well.

Ben Gottlieb
I'm looking for this coverflow sample in /Developer/Examples, without success, where can you find it?
Alex
It's actually "CovertFlow", and it's in /Developer/Examples/Quartz/Core Animation/"
Ben Gottlieb
A: 

Hey Ben,

Thanks for your comment, but I've already tried this. When I do a 3D rotation on my layer, the result is as if there's no depth perspective in the transform.

All the examples I look at seem to do a special 'sublayerTransform' on a layer to get the required effect - but it really isn't obvious what's going on here, and I've no idea how to apply it to a UIView.

Say I have a UIView and it's associated layer:

UIView *myView = [[self subviews] objectAtIndex:0];
CALayer *layer = myView.layer;

Does anyone know what I would have to do to to this layer to perform a perspective skew on the view?

Thanks again! Nick.

Nick Cartwright
+23  A: 

As Ben said, you'll need to work with the UIView's layer, using a CATransform3D to perform the layer's rotation. The trick to get perspective working, as described here, is to directly access one of the matrix cells of the CATransform3D (m34). Matrix math has never been my thing, so I can't explain exactly why this works, but it does. You'll need to set this value to a negative fraction for your initial transform, then apply your layer rotation transforms to that. You should also be able to do the following:

UIView *myView = [[self subviews] objectAtIndex:0];
CALayer *layer = myView.layer;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / -500;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;

which rebuilds the layer transform from scratch for each rotation.

A full example of this (with code) can be found here, where I've implemented touch-based rotation and scaling on a couple of CALayers, based on an example by Bill Dudney. The newest version of the program, at the very bottom of the page, implements this kind of perspective operation. The code should be reasonably simple to read.

The sublayerTransform you refer to in your response is a transform that is applied to the sublayers of your UIView's CALayer. If you don't have any sublayers, don't worry about it. I use the sublayerTransform in my example simply because there are two CALayers contained within the one layer that I'm rotating.

Brad Larson
Thanks Brad - you're a star. PS: Sorry for the late ticking of your excellent answer!Nick.
Nick Cartwright