代码之家  ›  专栏  ›  技术社区  ›  PvDev

如何在iOS swift的ARSCNView中显示uicollection视图

  •  0
  • PvDev  · 技术社区  · 6 年前

    我试图在arscnview中实现uicollection视图,并通过用户交互水平滚动方向。 在集合视图中,单元格包含产品图像和名称。是否可以将集合视图添加到CNView。

    我得到的结果是: enter image description here

    以下是我到目前为止读到的代码:

         let newCollection: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: 200, height: 100), collectionViewLayout: layout)
        layout.scrollDirection = .horizontal
        collection.backgroundColor = UIColor.gray
        collection.translatesAutoresizingMaskIntoConstraints = false
        collection.isScrollEnabled = true
        return collection
    }()
    
      func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    
        DispatchQueue.main.async {
            guard let imageAnchor = anchor as? ARImageAnchor,
                let imageName = imageAnchor.referenceImage.name else { return }
    
       self.newCollection.delegate = self
                self.newCollection.dataSource = self
                self.newCollection.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: self.cellId)
                self.view.addSubview(self.newCollection)
                self.setupCollection()
    
                let collectionPlane = SCNPlane(width: 0.2, height: 0.1)
                collectionPlane.firstMaterial?.diffuse.contents = self.newCollection
    
                let collectionNode = SCNNode(geometry: collectionPlane)
                collectionNode.eulerAngles.x = -.pi/2
                collectionNode.runAction(SCNAction.moveBy(x: 0, y: 0, z: 0, duration: 0))
    
                node.addChildNode(collectionNode)
    
    
                       self.sceneView.scene.rootNode.addChildNode(node)
    
    
       }
    
          //collection view
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 7
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = newCollection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyCollectionViewCell
        cell.backgroundColor = .blue
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        return CGSize(width: 100, height: 50)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    
        return UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
    }
    

    预期产出: enter image description here

    0 回复  |  直到 6 年前
        1
  •  0
  •   Lance Samaria    5 年前

    将材质漫反射的“内容”属性设置为collectionView的“视图”属性

    let material = SCNMaterial()
    material.diffuse.contents = yourCollectionView.view