我想用一个的UILabel添加自定义的UIView到SCNNode(使用SceneKit和ARKit)。
我用SCNPlane用含有创建的UIView的层SCNMaterial。然后我初始化新SCNNode并将其添加到场景中。首先,我试过的UIView在Interface Builder创建,这是行不通的。所以我决定创建的UIView programmaticaly。
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,一切工作正常。在哪里的问题?谢谢
有一次,我遇到这种情况。由于观点没有任何视图控制器的一部分,你必须手工绘制它:
view.setNeedsDisplay()
view.displayIfNeeded()