我正在尝试检测用户是否在 SKScene 中触摸屏幕的左侧或右侧。
我已将以下代码放在一起,但无论触摸何处,它都仅输出“Right”。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if(location.x > self.frame.size.width/2){
print("Left")
}
else if(location.x < self.frame.size.width/2){
print("Right")
}
}
}
最佳答案
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if(location.x < 0){
print("Left")
}
else {
print("Right")
}
}
}
这似乎有效。在您检查触摸是否位于屏幕左侧的左侧/右侧之前,因此它总是给您右侧。例如,在 iPhone 7 plus 上,您将检查您的触摸(假设 x 为 20)是否位于 365 的右侧或左侧。由于 20 小于 365,因此表示您单击了右侧。
关于ios - 快速检测屏幕左右两侧的触摸 SKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498047/