Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 938
Roi Mulia Avatar asked Jan 26 '26 18:01

Roi Mulia


2 Answers

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);
like image 89
Kyle Redfearn Avatar answered Jan 28 '26 07:01

Kyle Redfearn


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.

like image 29
thanhtrdang Avatar answered Jan 28 '26 07:01

thanhtrdang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!