gpt4 book ai didi

ios - 如何在 UIViews 和 SKNodes 之间转换点

转载 作者:可可西里 更新时间:2023-11-01 00:56:53 26 4
gpt4 key购买 nike

我想转换 CGPointUIView 之间和 SKNode ,类似于您如何通过各自的 View /图层在 View /图层之间进行转换 convert(_:to/from:)方法。

正确的做法是什么?

最佳答案

这是我想出的:

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)
}

关于ios - 如何在 UIViews 和 SKNodes 之间转换点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45252609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com