3

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

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)
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Tom
  • 3,672
  • 6
  • 20
  • 52

1 Answers1

2

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

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
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220