【发布时间】:2018-12-02 12:03:08
【问题描述】:
我希望在 ARKit 中的节点顶部保留一个 UILabel,类似于 iOS 12 的 Measure 应用程序中的尺寸标签。 我尝试将标签添加为平面节点并使用广告牌约束,但是当您离开时文本会变小,这并不理想。 在 WWDC,他们将其称为屏幕空间,但没有说明如何实现它。
提前致谢!
【问题讨论】:
标签: ios iphone swift uiview arkit
我希望在 ARKit 中的节点顶部保留一个 UILabel,类似于 iOS 12 的 Measure 应用程序中的尺寸标签。 我尝试将标签添加为平面节点并使用广告牌约束,但是当您离开时文本会变小,这并不理想。 在 WWDC,他们将其称为屏幕空间,但没有说明如何实现它。
提前致谢!
【问题讨论】:
标签: ios iphone swift uiview arkit
我想出了以下解决方案来让它发挥作用。
首先,我创建 UILabel 并将其添加为子视图。接下来,我将要跟随的节点的位置转换为renderer(_: updateAtTime:) 中的屏幕坐标。现在标签正确地跟随节点并保持固定的比例。但是,标签与屏幕保持水平,这看起来很奇怪。为了使其与世界保持水平,我根据 ARCamera 的偏航(z 旋转)旋转标签。
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
// Convert the node's position to screen coordinates
let screenCoordinate = self.sceneView.projectPoint(node.position)
DispatchQueue.main.async {
// Move the label
label.center = CGPoint(x: CGFloat(screenCoordinate.x), y: CGFloat(screenCoordinate.y))
// Hide the label if the node is "behind the screen"
label.isHidden = (screenCoordinate.z > 1)
// Rotate the label
if let rotation = sceneView.session.currentFrame?.camera.eulerAngles.z {
label.transform = CGAffineTransform(rotationAngle: CGFloat(rotation + Float.pi/2))
}
}
}
【讨论】:
node 和 centerVector?
尝试使用 SCNText 几何,几个月前我使用过以下代码:
var txtNote = SCNText()
txtNote.string = "Hello AR word!"
txtNote.font = UIFont.systemFont(ofSize: 2)
txtNote.extrusionDepth = 1.0
txtNote.containerFrame = CGRect(x: 2.5, y: 0, width: 16, height: 10)
txtNote.isWrapped = true
txtNote.alignmentMode = kCAAlignmentLeft
txtNote.materials.first?.diffuse.contents = UIColor.black.cgColor
var txtNode = SCNNode(geometry: txtNote)
【讨论】: