【发布时间】:2020-06-10 02:38:51
【问题描述】:
我正在尝试使用带有 Arkit 3D 空间的 UIView。我见过一些需要将 UIView 或 UIView 的层设置为 SCNMaterial 对象的漫反射内容的示例。到目前为止,这并没有像我预期的那样工作。我只看到视图的框架,但没有添加其中的子视图,图层上的角半径也不可见。
func setupBillBoard() {
let view = PlayerView(frame: CGRect(x: 0, y: 0, width: 70, height: 40))
view.backgroundColor = .red
view.layer.cornerRadius = 14
let material = SCNMaterial()
material.diffuse.contents = view.asImage()
material.isDoubleSided = true
let plane = SCNPlane(width: 1, height: 1)
plane.materials = [material]
let node = SCNNode()
node.geometry = plane
node.position = SCNVector3(box.x, box.y + 0.3, box.z - 0.4)
node.scale = SCNVector3(0.4, 0.4, 0.4)
self.billBoardNode = node
}
将 UIView 转换为图像捕获的视图扩展
extension UIView {
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
到目前为止,我在场景中只看到了一个红色方形视图,仅此而已
此处自定义类代码:https://www.paste.org/106551 附图中的xib
【问题讨论】:
-
也许你需要在创建视图后调用 view.layoIfNeeded() 。当我查看您的代码和下面的代码时,这是我看到的唯一区别。否则它看起来很好。如果您有任何问题,我会将代码发布到 GitHub,您可以将其加载到您的项目中并运行它。告诉我。
-
目前正在调试,我的自定义 UIView 还没有运气
-
我可以看到带有圆角的红色视图,这可能是我创建自定义 UIView 的方式
-
三件事。在创建 UIView 后,您首先调用 view.layoutIfNeeded() 吗?其次什么是PlayerView?也许该子类中存在导致问题的东西。第三次尝试将 PlayerView 更改为 UIView,看看会发生什么,然后切换回 PlayerView。
-
如果 PlayerView 不是机密的,请将其中的代码也添加到您的问题中。