Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we use CATransformLayer?

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.

like image 455
Evil Nodoer Avatar asked Feb 10 '14 07:02

Evil Nodoer


2 Answers

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,
enter image description here

But instead it looks like,
enter image description here

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.

like image 84
santhu Avatar answered Oct 14 '22 19:10

santhu


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.

like image 35
Wil Shipley Avatar answered Oct 14 '22 20:10

Wil Shipley