SwiftUI 调用 AR Quick Look 打开AR模型
1.您应该通过Xcode重写。
2.swift playground 打开,需要上传到 test flight 才能正常使用。
第一部分
第二部分
代码1
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
代码2
import SwiftUI
struct ContentView: View {
@State var ARshow = false
var body: some View {
ARShow(ARImage: "tutorial", ARText1: "1.1", ARText2: "tutorial", ARName: "tutorial", ARshow: ARshow)
//你只需要一行代码即可打开AR模型
//You only need one line of code to open the AR model
//ARShow(
}
}
代码3
import SwiftUI
import QuickLook
import ARKit
struct QLModel: UIViewControllerRepresentable {
var name: String
var allowScaling: Bool = true
func makeCoordinator() -> QLModel.Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
return controller
}
func updateUIViewController(_ controller: QLPreviewController, context: Context) { }
class Coordinator: NSObject, QLPreviewControllerDataSource {
let parent: QLModel
private lazy var fileURL: URL = Bundle.main.url(forResource: parent.name, withExtension: "reality")!//usdz
init(_ parent: QLModel) {
self.parent = parent
super.init()
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int { return 1 }
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
guard let fileURL = Bundle.main.url(forResource: parent.name, withExtension: "reality") else {
fatalError("Unable to load \(parent.name).reality from main bundle")}//usdz
let item = ARQuickLookPreviewItem(fileAt: fileURL)
item.allowsContentScaling = parent.allowScaling
return item
}
}
}
struct ARShow: View {
@Environment(\.colorScheme) var colorScheme//暗黑模式
var ARImage: String
var ARText1: String
var ARText2: String
var ARName: String
@State var ARshow = false
func simpleSuccess() {UINotificationFeedbackGenerator().notificationOccurred(.success)}
var body: some View {
HStack{
Button(action: {self.ARshow.toggle()}){
Image(ARImage)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 80, height: 80)
.background(Color.gray.opacity(0.1))
.cornerRadius(10, antialiased: true)
Text(ARText1)
.foregroundColor(.orange)
Text(ARText2)
.multilineTextAlignment(.leading)
.foregroundColor(colorScheme == .light ? .black : .white)
}
.fullScreenCover(isPresented: $ARshow){
ZStack{
QLModel(name: ARName).edgesIgnoringSafeArea(.all)
VStack{
Button(action: {self.ARshow.toggle(); simpleSuccess()}, label: {
HStack{
Image(systemName: "chevron.backward.circle")
.font(.title3)
Spacer()
}
})
.padding()
Spacer()
}
}
}
Spacer()
}
}
}