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!
Best Answer
As Ben said, you'll need to work with the
UIView's
layer, using aCATransform3D
to perform thelayer's
rotation
. The trick to get perspective working, as described here, is to directly access one of the matrixcells
of theCATransform3D
(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:Objective-C
Swift 5.0
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 yourUIView'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 twoCALayers
contained within the one layer that I'm rotating.