iosswiftobjectcalayercatransform3drotate

Understanding CATransform3DRotate / CATransform3DMakeRotation


I'm trying to understand how to create depth perceptive to my UIView like this :

enter image description here

I've tried this code:

var rotationWithPerspective = CATransform3DIdentity;
rotationWithPerspective.m34 = -1.0/500.0/2/2
rotationWithPerspective = CATransform3DRotate(rotationWithPerspective, 22.5, 0, 1, 0);
preview.layer.transform = rotationWithPerspective

which generates perspective. Yet, it Flips the View for some reason.

1) How can I avoid the "flipping" after the transform?

2) Does the "perspective depth" resulting from the transform, will be the same to every given UIView, or it depends on the UIView size, etc?

Thank YOU!


Solution

  • The CATransform3DRotate expects radians not degrees. You are rotating by 22.5 radians which is like 1200 degrees. That's probably why your image is inverted. You probably want to use this:

    let radians = 22.5 * M_PI / 180
    rotationWithPerspective = CATransform3DRotate(rotationWithPerspective, radians, 0, 1, 0);