我有一个使用SceneKit构建的应用程序,当前正在显示多个节点。我可以弄清楚按下了哪个节点,并希望使用它来使标签显示在被触摸的节点下方。现在,当我通过以下方式设置标签的中心时...

nameLabel.center = CGPointMake(CGFloat(result.node.position.x), CGFloat(result.node.position.y+20)


…由于节点位于(1, 0)上,因此它显示在左上角。我想的是,sceneView的(0, 0)位于屏幕的中央,而实际的物理显示器的(0, 0)位于左上角。

有没有办法将两个不同的数字相互转换? (我可以硬编码,因为我知道节点在哪里,或者为每个节点创建一个单独的标签,但这并不是一个完美的解决方案。)

提前致谢 :)

最佳答案

您可以使用projectPoint:方法:

var projected = view.projectPoint(result.node.position))
//projected is an SCNVector3
//projected.x and y are the node's position in screen coordinates
//projected.z is its depth relative to the near and far clipping planes
nameLabel.center = CGPointMake(CGFloat(projected.x), CGFloat(projected.y+20)

10-07 23:03