Question

I have been trying to perform a perspective transform on a UIView. I have been working from this example.

However, that example applies a rotation on the view as well as a perspective change. Is there a way to change the perspective of the view with no rotation? Something that might look like this:

enter image description here

I have been trying to remove the rotation, but when I do the perspective transform does not get applied. I can't find any examples out there that deal with just changing the perspective. Thanks in advance.

Was it helpful?

Solution

Just adding to David's answer: To get an output as in your image you have to rotate the view around the x-axis (the horizontal axis) so that the upper edge of the view rectangle appears "further away" from the viewer than the lower edge, e.g.

CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / -200;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0f * M_PI / 180.0f, 1.0f, 0.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;

OTHER TIPS

Perspective won't be visible without any rotation relative to your view point. The perspective does still get applied even when you don't rotate but it won't be very visible.

One other transform that shows perspective is translating along Z which makes the view look like it is being scaled.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top