core-animationtransformcalayercatransform3dcatransform3drotate

CALayer sublayerTransform (CATransform3D) - how can I move "camera"?


Attempt A

this code

CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = -1.0 / 1000.0;
perspectiveTransform = CATransform3DRotate(perspectiveTransform, angle / 2.0, 0.0, 1.0, 0.0);
self.sublayerTransform = perspectiveTransform;

gives this result

A

The red frame is the background color of the layer

Attempt B

This code

CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = -1.0 / 1000.0;
// following line is added 
perspectiveTransform = CATransform3DTranslate(perspectiveTransform, -width / 2.0, 0, 0);
perspectiveTransform = CATransform3DRotate(perspectiveTransform, angle / 2.0, 0.0, 1.0, 0.0);
self.sublayerTransform = perspectiveTransform;

gives this result

B

The red frame is the background color of the layer

So what is wrong?

In the rendered example from "Attempt A" is seen from left center and therefore the fold is seen from the wrong angle (left fold gets slightly thinner than right fold)

By translating it on the x-axis, as in "Attempt B", I am able to get it rendered correctly, but then occurs another mistake: the content is now out of bounds (red rectangle is bounds).

How can I make the camera be from the middle?


Solution

  • Well, it seems like changing the anchorpoint is the easiest. I just need to change some other transforms (not revealed in post) to make it work.