本文介绍了重置 ARView 并再次运行指导覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ARView,它会在 coachingOverlay 成功完成后立即填充.

I have an ARView which will be populated as soon as the coachingOverlay has finished successfully.

如何调用函数从另一个视图 (Navbar) 重置此 ARView 并运行 coachingOverlay 以再次初始化 AR?

How can I call a function to reset this ARView from another view (Navbar) and run coachingOverlay to initialize AR again?

struct ContentView : View {
    var body: some View {
        ZStack {
            ARViewContainer().edgesIgnoringSafeArea(.all)
            VStack {
                Spacer()
                Navbar()
            }
            .padding()
        }
    }
}

struct ARViewContainer: UIViewRepresentable {

    func makeUIView(context: Context) -> ARView {

        let arView = ARView(frame: .zero)

        arView.addCoaching()

        let config = ARWorldTrackingConfiguration()
        config.planeDetection = .horizontal
        arView.session.run(config, options: [])

        return arView

    }

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

}

extension ARView: ARCoachingOverlayViewDelegate {
    func addCoaching() {
        let coachingOverlay = ARCoachingOverlayView()
        coachingOverlay.delegate = self
        coachingOverlay.session = self.session
        coachingOverlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        coachingOverlay.goal = .anyPlane
        self.addSubview(coachingOverlay)
    }

    public func coachingOverlayViewDidDeactivate(_ coachingOverlayView: ARCoachingOverlayView) {
        // Load the "Box" scene from the "Experience" Reality File
        let boxAnchor = try! Experience.loadBox()

        // Add the box anchor to the scene
        self.scene.anchors.append(boxAnchor)
    }
}
struct Navbar: View {

    let arView = ARView()

    var body: some View {
        Button(action: {
            //
        }) {
            Image(systemName: "trash")
                .foregroundColor(Color.black)
                .padding()
        }
        .background(Color.white)
    }
}

推荐答案

要从 Navbar 调用指导叠加和跟踪配置,请使用 coordinator 属性.

For calling both coaching overlay and tracking config from Navbar use a coordinator property.

我想您不需要重置 ARView 本身.您需要做的就是使用以下选项重新运行会话:.resetTracking.removeExistingAnchors.它删除了以前的跟踪地图,您可以从头开始.

I suppose you don't need to reset ARView itself. All you need to do is to rerun a session with the following options: .resetTracking and .removeExistingAnchors. It removes a previous tracking map and you can begin from scratch.

arView.session.run(config, options: [.removeExistingAnchors, .resetTracking])

此外,如果您希望将指导覆盖视为单次操作,然后必须将其关闭,请使用以下设置:

Also, if you want to see a Coaching Overlay as a single-time operation and then it must be turned off use the following setting:

coachingOverlayView.activatesAutomatically = false

这篇关于重置 ARView 并再次运行指导覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:13