我在运行此代码时遇到了一些麻烦,我在另一则Stack Overflow
帖子中找到了它。 ARKit: How can I add a UIView to ARKit Scene?
想看看它是否对我有用。但是我无法弄清楚为什么会出现错误“ 不允许在顶层使用表达式”。
let text = "Hello, Stack Overflow."
let font = UIFont(name: "Arial", size: CGFloat(12))
let width = 128
let height = 128
let fontAttrs: [NSAttributedString.Key: Any] =
[NSAttributedString.Key.font: font as! UIFont]
let renderer = UIGraphicsImageRenderer(size: CGSize(width: CGFloat(width),
height: CGFloat(height)))
let image = renderer.image { context in
let color = UIColor.blue.withAlphaComponent(CGFloat(0.5))
color.setFill()
context.fill(CGRect(
origin: CGPoint(x: 0, y: 0),
size: UIScreen.main.bounds.size))
text.draw(with: (CGRect( origin: CGPoint(x: 0, y: 0), size:
UIScreen.main.bounds.size)), options: .usesLineFragmentOrigin, attributes:
fontAttrs, context: nil)
}
let plane = SCNPlane(width: CGFloat(0.1), height: CGFloat(0.1))
plane.firstMaterial?.diffuse.contents = image
let node = SCNNode(geometry: plane)
错误“不允许在顶层表达” 在显示以下代码的行上找到:
plane.firstMaterial?.diffuse.contents = image
先谢谢您。