我有一个使用SceneKit构建的应用程序,当前正在显示多个节点。我可以找出按下哪个节点,并希望使用它来使标签出现在被触摸的节点下方。现在,当我按照以下方式设置标签的中心时......
nameLabel.center = CGPointMake(CGFloat(result.node.position.x), CGFloat(result.node.position.y+20)
...它出现在左上角,因为节点在(1, 0)
上。我的想法是 sceneView&#39> <{strong> (0, 0)
位于屏幕的中心,而实际的物理显示& #39; s (0, 0)
位于左上角。
有没有办法将两个不同的数字相互转换? (我可以硬编码,因为我知道节点的位置或为每个节点创建一个单独的标签,但这不是一个完美的解决方案。)
提前致谢:)
答案 0 :(得分:1)
您可以使用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)