Swift - 在 3D 中旋转图像

时间:2021-04-18 08:53:23

标签: swift catransform3drotate

我正在根据 iPhone 的方向旋转 3D 图像,代码如下:它有效。

我的问题是:

  1. 这是一种可以接受的方法吗?

  2. 是否可以使用单个命令而不是依次构建三个 CATransform3DRotate。

  3. 我应该更改 .m34 值吗? 1.0 / -500 似乎用得很多,但我不太明白它的解释:https://en.wikipedia.org/wiki/3D_projection#Perspective_projection

       let layer = panelView.layer
       var rotationAndPerspectiveTransform = CATransform3DIdentity
       rotationAndPerspectiveTransform.m34 = 1.0 / -500
    
       rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, CGFloat(roll * .pi / 180), 0.0, 0.0, 1.0)    // roll
       rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, CGFloat(pitch * .pi / 180), 1.0, 0.0, 0.0)   // pitch
       rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, CGFloat(yaw * .pi / 180), 0.0, 1.0, 0.0)     // yaw
    
       UIImageView.animate(withDuration: 0.5, delay: 0, options: [], animations: {
            layer.transform = rotationAndPerspectiveTransform
       })
    

0 个答案:

没有答案