1

我正在尝试在 Reality Composer 的点击操作后更新 TEXT 信息。func textTouch()正在工作,我在调试时得到了打印。

self.newText = "new info"

如何更新视图?为什么状态变量只留在结构内部?

import SwiftUI
import RealityKit

struct ContentView : View {

    @State var newText: String = "Touch add more info"

    var body: some View {

        return VStack {
            ARViewContainer().edgesIgnoringSafeArea(.all)
            Text(newText)
        }
    }
}

struct ARViewContainer: UIViewRepresentable {

    func makeUIView(context: Context) -> ARView {

        let arView = ARView(frame: .zero)

        // Load the "Box" scene from the Reality File
        let birdAnchor = try! Cube.loadCube()

       let sunInfoAction = birdAnchor.actions.allActions.filter({$0.identifier == "cubeTap"}).first
       sunInfoAction?.onAction = { entity in

            self.textTouch()  
        }     
        arView.scene.anchors.append(birdAnchor)
        return arView 
    }

    func updateUIView(_ uiView: ARView, context: Context) {}

    func textTouch()  {
        print("working")
        self.newText = "new info"  
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
4

0 回答 0