Ios 将UIView中的位置转换为场景
我想在UIView中使用TouchStart功能,我应该触发SKScene方法。除了SKNode的启动位置与UIView中的触摸屏不同之外,一切都正常。我在这里读到了:但是他们没有给出一个解决方案,如何将UIView触摸手势转换为与触摸位置对应的SKScene触摸手势。这项工作:Ios 将UIView中的位置转换为场景,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我想在UIView中使用TouchStart功能,我应该触发SKScene方法。除了SKNode的启动位置与UIView中的触摸屏不同之外,一切都正常。我在这里读到了:但是他们没有给出一个解决方案,如何将UIView触摸手势转换为与触摸位置对应的SKScene触摸手势。这项工作: override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
var currentPoint = touch.location(in: scene)
executeSpawningStar(startPosition: currentPoint)
}
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
var currentPoint=touch.location(在:场景中)
executeSpawningStar(起始位置: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)
}
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
var currentPoint=触摸位置(在:newUIView中)
executeSpawningStar(起始位置:currentPoint)
}
}
产卵看起来很奇怪。产卵恒星的坐标不正确。如何将正确的坐标从UIView传输到SpriteKit设置,以匹配触摸手势的真实位置?好消息!SKView具有在SKView场景坐标之间进行转换的方法
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)
}
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
var currentPoint=触摸位置(在:newUIView中)
currentPoint=游戏场景.convertPoint(fromView:currentPoint)
executeSpawningStar(起始位置:currentPoint)
}
}
谢谢,但如何添加该功能?斯威夫特要我在上面加个{}。当我输入currentPoint=convert(currentPoint,to:gameScene)这行时,它表示使用未解析标识符convert。编辑:我成功了