I have read the documentation and several examples, but still cannot understand when exactly to use a CATransformLayer
. A normal CALayer
flattens everything at the z = 0
plane, but isn't that what a CATransformLayer
does too? A mathematical explanation would be very helpful.
To fully understand what are the differences between CALayer and CATransformLayer, you should read this article
It is the best tutorial which explains how to use CATransformLayer
.
But to say briefly,
Consider a scene(CAlayer instance
) where we create 4 planes at the same X, Y coordinates but with different Z and add it as subLayers
to scene.And rotate the scene by some angle.
So, we might expect it looks like,
But instead it looks like,
This is due to the fact that a CALayer is incapable of managing the depth of a 3D hierarchy and it just flattens the scene to a single Z level. To correct that depth issue, we use CATransformLayer
, it has depth understanding and renders based on it.
Note:images are taken from the article.
A CATransformLayer is used to transform its sublayers in 3D, and does NOT flatten them down.
Unlike normal layers, transform layers do not project (i.e. flatten) their sublayers into the plane at Z=0.
It’s something you’d use if you want to do a transform on a group of layers together but you didn’t want to do to each one individually because you wanted the flexibility of changing the transform in one place...kind of like a camera / world / model transform in OpenGL.
That said, I’ve never had occasion to use one, because I’ve never tried to do true 3D in LayerKit. I assume one was used for that stacking demo at WWDC a few years ago, there might be example code for that.
If I found myself starting to use a CATransformLayer nowadays, I’d switch to SceneKit, but that’s not on iOS yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With