如何将标签/ UIView 固定到 ARKit 中的节点

Leo*_*tar 4 iphone uiview ios swift arkit

我想让 UILabel 留在 ARKit 中的节点之上,类似于 iOS 12 的 Measure 应用程序中的尺寸标签。我尝试将标签添加为平面节点并使用广告牌约束,但是随着您移开,文本会变小,这并不理想。在 WWDC,他们将其称为 Screen Space,但没有说明如何实现它。

提前致谢! 在此处输入图片说明

Leo*_*tar 5

我想出了以下解决方案来使其工作。

首先,我创建 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))
    }
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 嘿 Leonard 你从哪里得到 `node` 和 `centerVector` 的? (3认同)