我想在一个简单的swiftUI页面和AR相机ContentView之间添加一个Reality Kit项目中的segue。这是我的SegueView,有一个按钮的迅捷页面,我想从AR视图中分离出来。
struct SegueView: View {
@State private var isActive: Bool = false
var body: some View {
NavigationLink(destination: ContentView(rootActive: $isActive), isActive: $isActive, label: {
Text("See in AR")
})
}
}下面是来自我的ContentView的代码,它只呈现一个框:
struct ContentView : View {
@Binding var rootActive: Bool
var body: some View {
return ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
// Load the "Box" scene from the "Experience" Reality File
let anchor = AnchorEntity(plane: .horizontal)
let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.3), materials: [SimpleMaterial(color: .blue, isMetallic: true)])
anchor.addChild(box)
// Add the box anchor to the scene
arView.scene.anchors.append(anchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}我按照这教程创建了segue,但是它不包含AR视图,我也找不到这样的教程。我还必须在这里添加应用程序委托中的rootActive变量:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView(rootActive: .constant(true))
// Use a UIHostingController as window root view controller.
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
return true
}但是由于本教程没有这一步,所以我不完全确定该做什么。当我运行这个应用程序时,只有ContentView会被呈现。有人知道我如何创建一个SegueView首先呈现的segue,然后当导航链接"see“被按下时,它会转到ContentView上吗?
谢谢!
发布于 2022-07-21 21:17:22
在ContentView中,将NavigationLink放在NavigationView中。
import SwiftUI
import RealityKit
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.black
VStack {
NavigationLink(destination: Reality().ignoresSafeArea()) {
Text("Go to ARView")
.foregroundColor(.cyan)
.font(.title3)
}
}
}.ignoresSafeArea()
}
}
}ARView
struct Reality: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let model = ModelEntity(mesh: .generateSphere(radius: 0.2))
let anchor = AnchorEntity(plane: .horizontal)
anchor.addChild(model)
arView.scene.anchors.append(anchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) { }
}P. S.
在项目的Info选项卡中添加键Privacy - Camera Usage Description。
https://stackoverflow.com/questions/73071348
复制相似问题