簡體   English   中英

在 Quicklook 中更改 AR model (usdz)

[英]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。

問題是

  • 當我通過選擇 collectionview 單元格更改產品時,Quicklook 自動切換到“對象”視圖,即使之前的 model 在 AR 屏幕(相機)中
  • AR 屏幕(相機)變為禁用 state。 我再也看不到 AR 屏幕上的新模型了。 我必須關閉 AR 屏幕才能響應 AR 模式。 或者有時我必須重新啟動應用程序(只是關閉 QLPreviewController 不適用於下一個 AR 視圖)。

這是 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM