如何在ARKit中绘制UIViews?

时间:2018-02-28 15:47:23

标签: arkit

我想将内容放在我通常在UIView中绘制的ARKit场景中。我希望实现这样的目标:

enter image description here

我知道如何在UIView中创建类似的东西,直截了当,但是,放置2D内容的正确步骤是什么,如3D图像中的图像所示,所以它看起来清晰,锐利且表现良好?< / p>

1 个答案:

答案 0 :(得分:4)

UIView可以呈现为SCNGeometry的素材。

这是一个非常基本的例子,可以帮助你入门:

/// Creates An SCNNode With A Plane Geometry & UIView As A Material
func createUIViewOnNode(){

    //1. Create An Empty Node
    let holderNode = SCNNode()

    //2. Set It's Geometry As An SCNPlane
    holderNode.geometry = SCNPlane(width: 0.6, height: 0.3)

    //2. Create A New Material
    let material = SCNMaterial()

    //3. Create A UIView As A Holder For Content
    let viewToAdd = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    viewToAdd.backgroundColor = .white

    //4. Create Two Subviews
    let redView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
    redView.backgroundColor = .red
    viewToAdd.addSubview(redView)

    let cyanView = UIView(frame: CGRect(x: 110, y: 10, width: 100, height: 100))
    cyanView.backgroundColor = .cyan
    viewToAdd.addSubview(cyanView)

    //5. Set The Materials Contents
    material.diffuse.contents = viewToAdd

    //6. Set The 1st Material Of The Plane
    holderNode.geometry?.firstMaterial = material
    material.isDoubleSided = true

    //7. Add To The Scene & Position It
    augmentedRealityView.scene.rootNode.addChildNode(holderNode)

    holderNode.position = SCNVector3(0, 0, -1.5)
}