Swift - Rotating Image in 3D

97 Views Asked by At

I am rotating an image in 3D depending on the orientation of the iPhone, with the following code: It works.

My questions are:

  1. Is this an acceptable approach?

  2. Is there are a single command I can use rather than building up three CATransform3DRotate in sequence.

  3. Should I alter the .m34 value? 1.0 / -500 seems to be used a lot but I don't really understand the explanation of it: 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

There are 0 best solutions below