[英]change AR model (usdz) in Quicklook
我正在開發具有 AR 功能的 iOS 購物應用程序。 用戶可以使用 iOS Quicklook 在 AR 中預覽產品。 AR 模型是 usdz 文件。
由於應用程序/設計要求,我不得不在 Quicklook 屏幕上添加自定義視圖。 用戶應該能夠 select AR Quicklook 屏幕中的顏色,因此我創建了一個從 QLPreviewController 派生的新視圖控制器並添加了一些 UI 組件(集合視圖、按鈕等)。 我對每種顏色都有不同的模型,所以當用戶選擇顏色時,應用程序應該加載不同的 model(usdz 文件)。 這是代碼片段。
class ARViewController: QLPreviewController {
override func viewDidLoad() {
// add UI elements here (collectionview, buttons, ..)
}
}
到目前為止一切都很好。 我可以打開 AR Quicklook 並可以第一次放置 object。
問題是
這是 QLPreviewDataSource 代碼
class ViewController: QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1 // I did try to update this number but did not worked
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
let productUrl = getProductUrl()
return productUrl as QLPreviewItem // I did try to pass ARQuickLookPreviewItem but same issue
}
}
當用戶在 Quiclook 中點擊 collectionViewCell 時,我剛剛更新了 AR model 名稱並調用了 QLPreviewController 的 reloadData function。
self.reloadData() // Also tried self.refreshCurrentPreviewItem() but same issue
任何幫助將不勝感激。 謝謝!
查看此解決方案以檢查您是否相應地實現了 QLPreviewController 委托方法。 您可以在 ARViewController 的擴展中使用它們來獲取下面發布的代碼。
除了上述之外,下面的代碼片段一定會有所幫助。 這不完全是你的情況,但它有效。 我在這里使用了 TableView controller 和一個名為 ARObjectTableViewCell 的單獨 XIB 文件。
class ARViewController: UITableViewController {
let models = ["model_01","model_02","model_03"]
var selectedIndex: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
let xib = UINib(nibName: "ARObjectTableViewCell", bundle: nil)
self.tableView.register(xib,
forCellReuseIdentifier: "objectDetailCell")
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return self.models.count
}
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ARObjectTableViewCell = tableView.dequeueReusableCell(
withIdentifier: "objectDetailCell",
for: indexPath) as! ARObjectTableViewCell
let modelName = models[indexPath.row]
cell.modelLabel.text = modelName.uppercased()
if let image = UIImage(named: "\(modelName)") {
cell.modelImageView.image = image
}
return cell
}
override func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150.0
}
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath,
animated: true)
self.selectedIndex = indexPath.row
let previewController = QLPreviewController()
previewController.delegate = self
previewController.dataSource = self
self.present(previewController, animated: true)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.