Hello everyone, I am requesting help on how I can pass variables between a coordinator class and a SwiftUI view.
This is my coordinator class:
class Coordinator: NSObject {
private let view: SCNView
init(_ view: SCNView) {
self.view = view
super.init()
}
@objc func search(sender: UITapGestureRecognizer) {
let sceneView = sender.view as! SCNView
let location = sender.location(in: sceneView)
let results = sceneView.hitTest(location, options: [SCNHitTestOption.searchMode : 1])
guard let hitTestResult = results.first else { return }
let finalnode = hitTestResult.node
var min = finalnode.boundingBox.min
var max = finalnode.boundingBox.max
@State var width = Double(max.x - min.x) //pass this variable to detail view
DetailView(width: $width)
let material = finalnode.geometry?.firstMaterial
if let material = material {
print("GREEN") //Prints "GREEN" every time, as expected
material.diffuse.contents = UIColor.green
}
}
}
This is my SwiftUI View:
struct DetailView: View {
@Binding var width: Double
var body: some View {
VStack{
CustomSceneView(scene:{
let scene = SCNScene(named: "Robot.usdz")!
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
// place the camera
cameraNode.position = SCNVector3(x: -4, y: 3, z: 5.5)
//cameraNode.rotation = SCNVector4(x: 0, y: -1, z: 0, w: 0.5)
cameraNode.rotation = SCNVector4(x: -1, y: -1.2, z: 0, w: .pi/4)
scene.rootNode.addChildNode(cameraNode)
return scene
}(), options: [.autoenablesDefaultLighting]).frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height/1.95,alignment: .top)
Text(String(width)) //Display the variable here
}.ignoresSafeArea(.all).edgesIgnoringSafeArea(.all)
}
}
How can I achieve the wanted result?