swift 按下按钮后如何使文本出现在屏幕上?

yyyllmsg  于 2023-01-04  发布在  Swift
关注(0)|答案(1)|浏览(122)

我想让文字在按下按钮后根据不同场景显示在屏幕上,比如显示型号A,屏幕上会出现文字"A",同理,显示型号B,屏幕上也会出现文字"B",我目前正在使用SwiftUI界面和RealityKit创建增强现实应用,但不确定下一步该怎么做。
下面是我的代码:

import SwiftUI
import RealityKit

struct ContentView : View {
    
    @State var arView = ARView(frame: .zero)
    
    var body: some View {
        ZStack {
            ARViewContainer(arView: $arView)
            HStack {
                Spacer()
                Button("information") {
                    print(self.arView.scene.name)
                    print(arView.scene.anchors.startIndex)
                    print(arView.scene.anchors.endIndex)
                }
                Spacer()
                Button("remove") {
                    stop()
                }
                Spacer()
            }
        } .edgesIgnoringSafeArea(.all)
    }
    func stop() {
        arView.scene.anchors.removeAll()
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    @Binding var arView: ARView
    
    func makeUIView(context: Context) -> ARView {
        
        let boxAnchor = try! Experience1.loadBox()
        let crownAnchor = try! Experience1.loadCrown()       
        arView.scene.anchors.append(boxAnchor)
        arView.scene.anchors.append(crownAnchor)        
        return arView  
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
}

在上面的代码中,如果boxAnchor和crownAnchor被显示,文本"Box"和"Crown"将分别出现在屏幕上。任何人谁知道如何做到这一点,请指导我或建议一个教程,我可以用来学习。
如果我用错了专业术语,很抱歉。谢谢

wribegjk

wribegjk1#

使用合并的React式订阅者和MVVM的绑定来更新文本视图的字符串值。

import SwiftUI
import RealityKit
import Combine

struct ContentView : View {
    
    @State private var arView = ARView(frame: .zero)
    @State private var str01: String = "...some text..."
    @State private var str02: String = "...some text..."
    
    var body: some View {
        ZStack {
            ARViewContainer(arView: $arView, str01: $str01, str02: $str02)
                .ignoresSafeArea()
            VStack {
                Spacer()
                Text(str01)
                    .foregroundColor(.white)
                    .font(.largeTitle)
                Divider()
                Text(str02)
                    .foregroundColor(.white)
                    .font(.largeTitle)
                Spacer()
            }
        }
    }
}

奇迹发生在subscribe(to:)示例方法的转义闭包中。if语句中的条件是什么取决于你。

struct ARViewContainer: UIViewRepresentable {
    
    @Binding var arView: ARView
    @Binding var str01: String
    @Binding var str02: String
    @State var subs: [AnyCancellable] = []
    
    func makeUIView(context: Context) -> ARView {
        let boxAnchor = try! Experience.loadBox()
        let crownAnchor = try! Experience.loadCrown()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            arView.scene.anchors.append(boxAnchor)
            arView.scene.anchors.append(crownAnchor)
        }
        return arView
    }
    func updateUIView(_ view: ARView, context: Context) {
        DispatchQueue.main.async {
            _ = view.scene.subscribe(to: SceneEvents.DidAddEntity.self) { _ in
                if view.scene.anchors.count > 0 {
                    if view.scene.anchors[0].isAnchored {
                        str01 = "Crown"
                        str02 = "Cube"
                    }
                }
            }.store(in: &subs)
        }
    }
}

相关问题