我想在CGPoint
和UIView
之间转换SKNode
,类似于您可以通过各自的convert(_:to/from:)
方法在视图/图层之间进行转换。
这样做的正确方法是什么?
答案 0 :(得分:3)
以下是我提出的建议:
extension SKNode {
/// Find point in node relative to it's containing SKView
/// so that it can be further converted relative to other views.
func pointInContainingView(_ point:CGPoint) -> (SKView, CGPoint)? {
guard let scene = scene, let view = scene.view else {
return nil
}
// Invert Y to match view coordinate system.
let invertedY = CGPoint(x: point.x, y: -point.y)
// Get the point in it's containing scene.
let pointInScene = convert(invertedY, from: scene)
// Get the resulting point in the containing SKView
let pointInView = view.convert(pointInScene, from: scene)
return (view, pointInView)
}
}
像这样使用:
// view/node
let origin = node.frame.origin
if let (nodeView, point) = node.parent?.pointInContainingView(origin) {
let converted = nodeView.convert(point, from:view)
}
答案 1 :(得分:0)
您需要做的就是range
如果您需要转换不是场景的子节点但是场景的后代的节点,您可以:
scene.convertPoint(toView:pointOnScene)
使用的一个很好的小扩展:
let absolutePosition = node.convert(node.position,toNode:node.scene)
scene.convertPoint(toView: absolutePosition)