3

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!

QED
  • 9,803
  • 7
  • 50
  • 87
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105

2 Answers2

4

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);
Kyle Redfearn
  • 2,172
  • 16
  • 34
  • Thanks Kyle! BTW, do you have an answer for my second question? is it "global perspective" values or it's per UIView? – Roi Mulia Feb 17 '16 at 04:29
1

For 2)

  • It's per UIView.
  • In case you wanna apply to sublayers, you can use sublayerTransform. E.g. preview.layer.sublayerTransform = rotationWithPerspective

You can read more about it in iOS Core Animation - Advanced Techniques by Nick Lockwood. Hope that help.