SwiftUI Gestures like MagnificationGesture seem well targeted to manipulating a View. In an app using SceneKit, I am usually most interested in manipulating the SceneView’s scene (SCNScene type) contents. As in this example, in which I’m using MagnificationGesture to change the FOV of SceneView’s pointOfView node's camera. What I’ve coded works, but I long ago learned that working code doesn’t mean it works well or performantly.
So my question is whether I am doing the MagnificationGesture correctly to change the FOV of the camera in the pointOfView node? Please use simple words in small sentences in your advice and criticisms.
So my question is whether I am doing the MagnificationGesture correctly to change the FOV of the camera in the pointOfView node? Please use simple words in small sentences in your advice and criticisms.
Code Block swift import SwiftUI import SceneKit struct ContentView: View { @State private var magnify = CGFloat(1.0) @State private var doneMagnifying = false @GestureState var magnifyBy = CGFloat(1.0) var magnification: some Gesture { MagnificationGesture() .updating($magnifyBy) { (currentState, gestureState, transaction) in gestureState = currentState print("magnifyBy = \(self.magnifyBy)") } .onChanged{ (value) in self.magnify = value } .onEnded { _ in self.doneMagnifying = true } } // The camera node for the scene. var cameraNode: SCNNode? { get{ var node = SCNNode() node = aircraftScene.rootNode.childNode(withName: "distantCameraNode", recursively: true)! let maximumFOV: CGFloat = 25 // This is what determines the farthest point into which to zoom. let minimumFOV: CGFloat = 90 // This is what determines the farthest point from which to zoom. let currentFOV = node.camera?.fieldOfView if !doneMagnifying { print("Still magnifying") node.camera?.fieldOfView *= magnifyBy } else { print("Done magnifying. Yippeee!!!") node.camera?.fieldOfView *= magnify } if node.camera!.fieldOfView <= maximumFOV { node.camera!.fieldOfView = maximumFOV } if node.camera!.fieldOfView >= minimumFOV { node.camera!.fieldOfView = minimumFOV } return node } } var aircraftScene: SCNScene { get { print("Loading Scene Assets.") guard let scene = SCNScene(named: "art.scnassets/ship.scn") else { print("Oopsie, no scene") return SCNScene() } return scene } } var body: some View { ZStack { Color.black.edgesIgnoringSafeArea(.all) SceneView ( scene: aircraftScene, pointOfView: cameraNode, options: [] ) .background(Color.black) .gesture(magnification) VStack() { Text("Hello, SceneKit!").multilineTextAlignment(.leading).padding() .foregroundColor(Color.gray) .font(.largeTitle) Text("Pinch to zoom.") .foregroundColor(Color.gray) .font(.title) Spacer(minLength: 300) } } } }