如何通过点击RealityKit中的实体来打开SwiftUI视图?

efzxgjgh  于 2023-01-08  发布在  Swift
关注(0)|答案(1)|浏览(138)

我正在RealityKit中使用SwiftUI。如下面的代码所示,我有一个平面实体,当点击它时,它只打印实体的名称。当我点击实体时,我应该采取什么方法来导航到一个新的视图?最好像普通视图中的导航链接一样导航,但如果这不可能,那么也许是一个完整的屏幕封面?
ARViewContainer.swift:

class Coordinator: NSObject {
    
    weak var view: ARView?
    
    @objc func handleTap(_ recognizer: UITapGestureRecognizer) {
        
        guard let view = self.view else { return }
        
        let tapLocation = recognizer.location(in: view)
        
        if let entity = view.entity(at: tapLocation) as? ModelEntity {
            print(entity.name)
        }
        
    }
    
}

struct ARViewContainer: UIViewRepresentable {
    typealias UIViewType = ARView
    

    func makeUIView(context: Context) -> ARView{
        let arView = ARView(frame: .zero, cameraMode: .ar, automaticallyConfigureSession: true)
        
        context.coordinator.view = arView
                
        arView.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleTap)))
        
        arView.scene.anchors.removeAll()
        

        let anchor = AnchorEntity()

        let plane = MeshResource.generatePlane(width: 1, height: 1)

        
        
        var material = UnlitMaterial()
        material.color = .init(tint: .white,
                            texture: .init(try! .load(named: "instagram")))
       

        let planeEntity = ModelEntity(mesh: plane, materials: [material])
        planeEntity.generateCollisionShapes(recursive: true)
        
        planeEntity.name = "Plane Entity"

        planeEntity.position.z -= 1.0
        planeEntity.setParent(anchor)
        arView.scene.addAnchor(anchor)
        
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context){
        
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
}

ContentView.swift

struct ContentView: View {   
    @State var open = false
    
    var body: some View {
        NavigationView{
            ZStack {
                ARViewContainer()
                    .ignoresSafeArea(.all)
            }
        } 
    }
}

我要导航到的视图:

struct TestView : View {
    var body : some View {
        VStack{
            Text("Test View")
        }
    }
}
5gfr0r5j

5gfr0r5j1#

管理可观察对象中的视图状态,并从AR视图中对其进行修改。

struct ContentView: View {
    @ObservedObject var settings = Settings.shared

    var body: some View {
        NavigationView {
            ZStack {
                ARViewContainer()
                    .ignoresSafeArea(.all)

                NavigationLink("", isActive: $settings.shouldOpenDetailsView) {
                    TestView()
                }
            }
        }
    }
}
class Settings: ObservableObject {
    static let shared = Settings()

    @Published var shouldOpenDetailsView = false
}
class Coordinator: NSObject {
    weak var view: ARView?

    @objc func handleTap(_ recognizer: UITapGestureRecognizer) {
        guard let view = self.view else { return }

        let tapLocation = recognizer.location(in: view)

        if let entity = view.entity(at: tapLocation) as? ModelEntity {
            Settings.shared.shouldOpenDetailsView = true
        }
    }
}

相关问题