理解 CATransform3DRotate / CATransform3DMakeRotation

Understanding CATransform3DRotate / CATransform3DMakeRotation

我正在尝试了解如何创建对我的 UIView 的深度感知,如下所示:

我试过这个代码:

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

生成视角。然而,由于某种原因,它翻转 视图。

1) 如何避免转换后的"flipping"?

2) 变换产生的 "perspective depth" 是否与每个给定的 UIView 相同,还是取决于 UIView 大小等?

谢谢!

CATransform3DRotate 需要弧度而不是度数。您正在旋转 22.5 弧度,相当于 1200 度。这可能就是为什么你的图像是倒置的。你可能想使用这个:

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

对于 2)

  • 根据 UIView。
  • 如果你想应用到子层,你可以使用 sublayerTransform。 例如。 preview.layer.sublayerTransform = 旋转透视

您可以在 Nick Lockwood 的 iOS Core Animation - Advanced Techniques 中阅读更多相关信息。希望有所帮助。