如何在场景中添加按钮和标签

时间:2019-05-22 09:39:00

标签: ios swift uikit scenekit arkit

我是Scenekit和ARKit的新手,我想在UIView中添加按钮和标签到Scenekit的场景。我通过点击测试获取世界坐标以放置视图,但是SCNNode没有addSubView方法来添加视图。

我想实现以下输出:

enter image description here

我为实现这一目标的尝试:

func addHotspot(result : SCNHitTestResult, parentNode : SCNNode? = nil) {
    let view = UIView()
    view.backgroundColor = .red

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

    let plane = SCNPlane(width: 100, height: 100)
    plane.materials = [material]

    let node = SCNNode()
    node.geometry = plane

    node.position = result.worldCoordinates
    parentNode?.addChildNode(node)
}

错误:

enter image description here

请提出我的完成方式。

1 个答案:

答案 0 :(得分:0)

要将UIView对象添加到场景节点,可以将其分配给节点的几何材料漫射内容。它会被添加到节点,并在您移动设备时保持其位置:

DispatchQueue.main.async {
  let view: UIView = getView() // this is your UIView instance that you want to add to the node

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

  node.geometry.materials = [material]
}