【问题标题】:Added custom UIView into SCNNode not shown properly将自定义 UIView 添加到 SCNNode 中未正确显示
【发布时间】:2019-06-29 18:52:10
【问题描述】:

我想将带有一个 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)
    }
}

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

【问题讨论】:

    标签: swift uiview scenekit arkit


    【解决方案1】:

    有一次我遇到了这个案子。由于视图不是任何视图控制器的一部分,因此您必须手动绘制它:

    view.setNeedsDisplay()
    view.displayIfNeeded()
    

    【讨论】:

    • 我添加了这个方法但结果相同:(
    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多