HI, I'm new to IOS Dev.
I am developing an app with AR function. I found there are a few tutorials about AR Quick Look. However, they're all use storyboard. Is there any way to use swift ui to demonstrate AR Quick Look.
ContentView.swift
import SwiftUI
//import QuickLook
//import ARKit
struct ContentView: View {
@State private var isPresented = false
var body: some View {
VStack {
Button {
isPresented = true
print("click")
} label: {
Text("Click to AR")
.font(.title)
.fontWeight(.bold)
.padding()
.background()
.cornerRadius(16)
}
.sheet(isPresented: $isPresented) {
ARView()
}
.padding()
}
}
}
#Preview {
ContentView()
}
ARView.swift
import SwiftUI
struct ARView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> QuickViewController {
QuickViewController()
}
func updateUIViewController(_ uiViewController: QuickViewController, context: Context) {
uiViewController.presentARQuickLook()
}
typealias UIViewControllerType = QuickViewController
}
QuickViewController.swift
import UIKit
import QuickLook
import ARKit
class QuickViewController: UIViewController, QLPreviewControllerDelegate,
QLPreviewControllerDataSource {
// 有幾個模型要呈現
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
// 顯示模型
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
let url = Bundle.main.url(forResource: "bear", withExtension: "usdz")!
// Load file url
let preview = ARQuickLookPreviewItem(fileAt: url)
return preview
}
func presentARQuickLook() {
let previewController = QLPreviewController()
previewController.dataSource = self
present(previewController, animated: true)
print("Open AR model!")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}