0

I am loading a 3D object using SceneView in SwiftUI:

SceneView(
            scene: scene,
            pointOfView: cameraNode,
            options: [.autoenablesDefaultLighting]
        )

I have removed .allowsCameraControl property from options to handle gestures manually. I want to be able to rotate the object horizontally. In UIKit I was able to do so using UIPanGestureRecognizer like this:

func rotateObject(_ gesture: UIPanGestureRecognizer) {
    var currentAngleY: Float = 0.0
    guard let nodeToRotate = scene?.rootNode else { return }
    let translation = gesture.translation(in: gesture.view!)
    var newAngleY = (Float)(translation.x) * (Float)(Double.pi) / 180.0
    newAngleY += currentAngleY
    nodeToRotate.eulerAngles.y = newAngleY
    if(gesture.state == .ended) { currentAngleY = newAngleY }
}

But in SwiftUI I am not sure how can do it, any help would be appreciated.

Mc.Lover
  • 4,813
  • 9
  • 46
  • 80

1 Answers1

0

The full story behind the solution you find in this article. The code in SwiftUI.

Circle()
.fill(Color.white)
.opacity(0.1)
.frame(width: 256, height: 256, alignment: .center)

.gesture(
    DragGesture(minimumDistance: 30, coordinateSpace: .global)
        .onChanged { gesture in
            startWalking.send(gesture.translation)
        }
        .onEnded { _ in
            stopWalking.send(true)
        }
)

You can use a drag gesture and a subscription to transfer the information between your sceneView and the SwiftUI interface.

user3069232
  • 8,587
  • 7
  • 46
  • 87