Swift 中令人困惑的反向触摸事件
Confusing reversed touch event in Swift
在我的函数中
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
for touch in touches{
let touchLocation = touch.location(in: self.view)
print("X: \(touchLocation.x) Y: \(touchLocation.y)")
}
}
我得到了颠倒的 Y 值。如果我点击顶部,我得到 ~0 值,如果我点击底部,我得到高值。这使我的精灵在错误的 Y 方向上移动。
但是,如果我在 self.view 中删除“.view”,它就会正常工作。有谁知道为什么我用self.view的时候反了?
正如 Whirlwind 在您的问题下方的评论中所解释的那样,发生这种情况是因为 self
您指定 SKScene
(SpriteKit) 而 self.view
您使用 SKView
(UIView
子类所以 UIKit 系统):
UIKit坐标系iOS原点在绘图区左上角,正值延伸从它向下和向右。
SpriteKit坐标系在iOS和[=34=上使用相同的坐标系] X,原点(0,0)在左下角,坐标(1334,750)在右上角。
在我的函数中
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
for touch in touches{
let touchLocation = touch.location(in: self.view)
print("X: \(touchLocation.x) Y: \(touchLocation.y)")
}
}
我得到了颠倒的 Y 值。如果我点击顶部,我得到 ~0 值,如果我点击底部,我得到高值。这使我的精灵在错误的 Y 方向上移动。
但是,如果我在 self.view 中删除“.view”,它就会正常工作。有谁知道为什么我用self.view的时候反了?
正如 Whirlwind 在您的问题下方的评论中所解释的那样,发生这种情况是因为 self
您指定 SKScene
(SpriteKit) 而 self.view
您使用 SKView
(UIView
子类所以 UIKit 系统):
UIKit坐标系iOS原点在绘图区左上角,正值延伸从它向下和向右。
SpriteKit坐标系在iOS和[=34=上使用相同的坐标系] X,原点(0,0)在左下角,坐标(1334,750)在右上角。