我想在我的Reality Kit项目中,在一个简单的swiftUI页面和AR摄像机内容视图之间添加一个segue.这是我的SegueView,swiftui页面上有一个按钮,我想将其切换到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) {} 
}

我按照this教程创建了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.有谁知道我如何创建一个segue,首先呈现SegueView,然后当按下导航链接"在AR中查看"时,它会转到ContentView?

谢谢

推荐答案

在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.

Swift相关问答推荐

如何在SWIFT中解码Instagram Backup中的字符串?

不使用`lockFocus`将两个重叠的NSImage转换为单个NSImage

如何在SWIFT中轻松格式化公制和英制使用的UnitVolume

OBJC代码中Swift 演员的伊瓦尔:原子还是非原子?

我在SwiftUI中的动画无法正常工作

无论玩家移动到视图内的哪个位置,如何使用 SpriteKit 让敌人向玩家emits 子弹?

避免 `(())` 显式指定 Void 类型的枚举关联值

快速更改tableView中的数据

如何从另一个 swift 文件中调用函数

数组使用偏移量对自身执行 XOR

Swift Components.Url 返回错误的 URL

SwiftUI:当先前的视图列表更改时,NavigationView 详细视图会弹出回栈

AVPlayer 在 iOS 15.4 中寻求 completionHandler 返回 false

Swift中的分段错误

临时添加到视图层次 struct 后,Weak view引用不会被释放

Swift 2 中的新 @convention(c):我该如何使用它?

Swift 中的 PageViewController 当前页面索引

Swift - 导入我的 swift 类

自定义 MKAnnotation 标注视图?

playground 导入:没有这样的模块Foo