【发布时间】: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