在SCNNode中添加了自定义UIView,无法正确显示

时间:2019-02-05 21:13:42

标签: swift uiview scenekit arkit

我想将带有一个UILabel的自定义UIView添加到SCNNode中(使用SceneKit和ARKit)。

我将SCNPlane与SCNMaterial一起使用,其中包含创建的UIView图层。然后,我初始化新的SCNNode并将其添加到场景中。首先,我尝试了在界面生成器中创建的UIView,但这是行不通的。因此,我决定以编程方式创建UIView。

class InfoPlaneNodeView: UIView {
    var elevationLabel: UILabel

    override init(frame: CGRect) {
        elevationLabel = UILabel(frame: frame)
        super.init(frame: frame)
        backgroundColor = UIColor.white
        layer.cornerRadius = 20

        elevationLabel.translatesAutoresizingMaskIntoConstraints = false
        elevationLabel.textColor = UIColor.black
        elevationLabel.text = "333m"
        addSubview(elevationLabel)

        elevationLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        elevationLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    }
}

class InfoViewPlaneNode: SCNNode {
    init(planeWidth: CGFloat, planeHeight: CGFloat) {
        let material = SCNMaterial()
        let infoPlaneNodeView = InfoPlaneNodeView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
        material.diffuse.contents = infoPlaneNodeView.layer
        material.transparency = 0.7
        material.lightingModel = .constant
        material.isDoubleSided = true

        let plane = SCNPlane(width: planeWidth, height: planeHeight)
        plane.materials = [material]
        super.init()
        self.geometry = plane
        self.position = SCNVector3(0, 3, 0)
    }
}

运行所有代码时,我只会看到白色的UIView而没有UILabel。我在没有SceneKit的情况下尝试了此代码,并且一切正常。问题在哪里?谢谢

https://imgur.com/a/rQmKJX6

1 个答案:

答案 0 :(得分:0)

一旦我遇到这种情况。由于视图不是任何视图控制器的一部分,因此必须手动绘制它:

view.setNeedsDisplay()
view.displayIfNeeded()
相关问题