我想在 UIView 中使用 touchBegan 功能,并且应该触发 SKScene 方法。一切都几乎正常,除了我的 SKNode 的起始位置与 UIView 中的触摸不同之外。我在这里读到:Swift 中令人困惑的反向触摸事件 https://stackoverflow.com/questions/41172551/confusing-reversed-touch-event-in-swift但是他们没有给出如何将 UIView 触摸手势转换为与触摸位置对应的 SKScene 触摸手势的解决方案。这有效:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
var currentPoint = touch.location(in: scene)
executeSpawningStar(startPosition: currentPoint)
}
}
但是我在它上面放了一个UIView,这个功能就不起作用了。当我把它改成这样时:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
var currentPoint = touch.location(in: newUIView)
executeSpawningStar(startPosition: currentPoint)
}
}
产卵看起来非常奇怪。产卵星的坐标不正确。如何将正确的坐标从 UIView 传输到 SpriteKit 设置以匹配触摸手势的真实位置?
好消息! SKView 具有与 SKScene 坐标相互转换的方法。
func convert(_ point: CGPoint, from scene: SKScene) -> CGPoint
func convert(_ point: CGPoint, to scene: SKScene) -> CGPoint
所以你可以这样做:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
var currentPoint = touch.location(in: newUIView)
currentPoint = gameScene.convertPoint(fromView: currentPoint)
executeSpawningStar(startPosition: currentPoint)
}
}
https://developer.apple.com/reference/spritekit/skview/1520328-convert https://developer.apple.com/reference/spritekit/skview/1520328-convert
https://developer.apple.com/reference/spritekit/skview/1519847-convert https://developer.apple.com/reference/spritekit/skview/1519847-convert
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)