在 Spritekit 中区分滑动和触摸 - Swift 3
Distinguish Swipe from Touch in Spritekit - Swift 3
我目前正在开发一款街机应用程序,用户可以通过点击让精灵跳过障碍物,然后向下滑动让它滑过障碍物。我的问题是,当我开始滑动时,会调用 touchesBegan 函数,因此精灵会跳跃而不是滑动。有办法区分这两者吗?
您可以使用手势状态来微调用户交互。手势是协调的,所以应该不会互相干扰。
func handlePanFrom(recognizer: UIPanGestureRecognizer) {
if recognizer.state != .changed {
return
}
// Handle pan here
}
func handleTapFrom(recognizer: UITapGestureRecognizer) {
if recognizer.state != .ended {
return
}
// Handle tap here
}
为您的触摸控件设置一个轻微的延迟怎么样?我有一个游戏,我在其中使用 SKAction 延迟执行类似的操作。您可以选择设置一个位置 属性 以使用 touchesMoved 方法给自己一些回旋余地,以防有人手指抽搐(感谢 KnightOfDragon)
let jumpDelayKey = "JumpDelayKey"
var startingTouchLocation: CGPoint?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
// starting touch location
startingTouchLocation = location
// start jump delay
let action1 = SKAction.wait(forDuration: 0.05)
let action2 = SKAction.run(jump)
let sequence = SKAction.sequence([action1, action2])
run(sequence, withKey: jumpDelayKey)
}
}
func jump() {
// your jumping code
}
只要确保延迟不会太长,这样您的控件就不会反应迟钝。尝试使用您想要的结果的值。
如果达到移动阈值,则在您的 touches moved 方法中移除 SKAction
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
guard let startingTouchLocation = startingTouchLocation else { return }
// adjust this value of how much you want to move before continuing
let adjuster: CGFloat = 30
guard location.y < (startingTouchLocation.y - adjuster) ||
location.y > (startingTouchLocation.y + adjuster) ||
location.x < (startingTouchLocation.x - adjuster) ||
location.x > (startingTouchLocation.x + adjuster) else {
return}
// Remove jump action
removeAction(forKey: jumpDelayKey)
// your sliding code
}
}
您可以尝试使用手势识别器,但我不确定它是否有效以及它如何影响响应链。
希望对您有所帮助
我目前正在开发一款街机应用程序,用户可以通过点击让精灵跳过障碍物,然后向下滑动让它滑过障碍物。我的问题是,当我开始滑动时,会调用 touchesBegan 函数,因此精灵会跳跃而不是滑动。有办法区分这两者吗?
您可以使用手势状态来微调用户交互。手势是协调的,所以应该不会互相干扰。
func handlePanFrom(recognizer: UIPanGestureRecognizer) {
if recognizer.state != .changed {
return
}
// Handle pan here
}
func handleTapFrom(recognizer: UITapGestureRecognizer) {
if recognizer.state != .ended {
return
}
// Handle tap here
}
为您的触摸控件设置一个轻微的延迟怎么样?我有一个游戏,我在其中使用 SKAction 延迟执行类似的操作。您可以选择设置一个位置 属性 以使用 touchesMoved 方法给自己一些回旋余地,以防有人手指抽搐(感谢 KnightOfDragon)
let jumpDelayKey = "JumpDelayKey"
var startingTouchLocation: CGPoint?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
// starting touch location
startingTouchLocation = location
// start jump delay
let action1 = SKAction.wait(forDuration: 0.05)
let action2 = SKAction.run(jump)
let sequence = SKAction.sequence([action1, action2])
run(sequence, withKey: jumpDelayKey)
}
}
func jump() {
// your jumping code
}
只要确保延迟不会太长,这样您的控件就不会反应迟钝。尝试使用您想要的结果的值。
如果达到移动阈值,则在您的 touches moved 方法中移除 SKAction
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
guard let startingTouchLocation = startingTouchLocation else { return }
// adjust this value of how much you want to move before continuing
let adjuster: CGFloat = 30
guard location.y < (startingTouchLocation.y - adjuster) ||
location.y > (startingTouchLocation.y + adjuster) ||
location.x < (startingTouchLocation.x - adjuster) ||
location.x > (startingTouchLocation.x + adjuster) else {
return}
// Remove jump action
removeAction(forKey: jumpDelayKey)
// your sliding code
}
}
您可以尝试使用手势识别器,但我不确定它是否有效以及它如何影响响应链。
希望对您有所帮助