Search code examples
iosobjective-ccore-animationcatransform3dperspectives

Apply a perspective transform without rotating


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.


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;