In Main View, I created 2 buttons, Scene1 and Scene2.
- If Scene1 is clicked, it will navigate to the Content View and show the AR Experience in Scene1.
- If Scene2 is clicked, it will navigate to the Content View and show the AR Experience in Scene2.
Main View
import SwiftUI
struct Main: View {
@State private var Scene = ""
var body: some View {
NavigationStack{
HStack {
Spacer()
NavigationLink(destination: ContentView(Scene: $Scene)){
Text("Scene1")
}
.simultaneousGesture(TapGesture().onEnded{
Scene = "1"
})
Spacer()
NavigationLink(destination: ContentView(Scene: $Scene)){
Text("Scene2")
}
.simultaneousGesture(TapGesture().onEnded{
Scene = "2"
})
Spacer()
}
}
}
}
Content View and ARViewCotainer
struct ARViewContainer: UIViewRepresentable {
let model: AppModel
@Binding var Scene: String
func makeCoordinator() -> Coordinator {
Coordinator()
}
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
arView.scene.anchors.removeAll()
let Scene1 = try! Experience1.loadScene1()
let Scene2 = try! Experience1.loadScene2()
if Scene == "1" {
arView.scene.anchors.append(Scene1)
} else if Scene == "2" {
arView.scene.anchors.append(Scene2)
}
model.playStory.sink {
Scene1.notifications.replay.post()
Scene2.notifications.replay.post()
}.store(in: &context.coordinator.subscriptions)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
class Coordinator: NSObject {
var subscriptions = Set<AnyCancellable>()
}
}
From my code, app can work properly. However, I would like to use arView
in Content View for any activities e.g. arView.scene.anchors.count > 0
or arView.snapshot(saveToHDR: false)
.
When I declared arView like @State private var arView = ARView(frame: .zero)
in Content View and binded to the ARViewContainer, the camera was not showed. How to fix this problem.