Swift 游戏 - 点击并点击 + 按住手势?

Swift Game - Tap and Tap + Hold Gestures?

我正在学习 swift(和 spritekit)并尝试制作一个简单的游戏。

在我的游戏中,英雄应该跳跃或躲避...

英雄需要在点击屏幕时跳跃,或者在点击+按住屏幕时躲避(long gesture)

所以基本的伪代码:

if tapped
    heroJump()
else if tappedAndHeld
    heroDuck()

我有一个func几乎在所有教程中都能看到,它处理触摸事件:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch in touches {
let location = touch.locationInNode(self) //need location of tap for something

    switch gameState {
        case .Play:
            //do in game stuff

            break
        case .GameOver:
            //opps game ended

            }
            break
    } 
 }
   super.touchesBegan(touches, withEvent: event)
}

有没有办法在这个触摸事件中包含它来决定它是被点击还是被按住?我似乎无法理解这个事实,程序总是会在长手势之前识别轻击?!?

无论如何,为了解决我的问题,我发现了 问题,它向我介绍了我试图实现的识别器:

override func didMoveToView(view: SKView) {
    // other stuff

    //add long press gesture, set to start after 0.2 seconds
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
    longPressRecognizer.minimumPressDuration = 0.2
    self.view!.addGestureRecognizer(longPressRecognizer)

    //add tap gesture
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
    self.view!.addGestureRecognizer(tapGestureRecognizer)
}

这些是手势调用的函数:

func longPressed(sender: UILongPressGestureRecognizer)
{
    if (sender.state == UIGestureRecognizerState.Ended) {
        print("no longer pressing...")
    } else if (sender.state == UIGestureRecognizerState.Began) {
        print("started pressing")
        // heroDuck()
    }
}

func tapped(sender: UITapGestureRecognizer)
{
    print("tapped")
    // heroJump()
}

如何将这两个东西结合起来? 我可以在我的 touchBegins 事件中添加一种方法来确定它是被点击还是按住,或者我可以放弃该方法并仅使用上面的两个函数吗?

如果使用后者,获取位置会有很多问题吗?

或者也许我完全看错了,在 swift/spritekit?

中有一个简单的 and/or 内置方式

谢谢。

你为长按做同样的事情,等到 .Ended 事件

func tapped(sender: UITapGestureRecognizer)
{

    if sender.state == .Ended {
      print("tapped")
    }
}

点击事件总是会发生,这是无法避免的,因为面对现实吧,您需要触摸屏幕。但是应该发生的是当您进入长按事件时,点击事件应该进入取消状态而不是结束状态

您只需要 UITapGestureRecognizerUILongPressRecognizer。您无需对 touchesBegantouchesEnded 执行任何操作,因为手势识别器会自行分析触摸。

要获取触摸的位置,您可以在手势识别器上调用 locationInView 来获取手势的位置,或者 locationOfTouch 如果您正在使用多点触控手势并且需要每个手势的位置触碰。当您想要 window 的基本坐标系中的坐标时,将 nil 作为参数传递。

这是一个工作示例:

func setupRecognizers() {
     let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
     let longTapRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("handleLongPress:"))
     view.addGestureRecognizer(tapRecognizer)
     view.addGestureRecognizer(longTapRecognizer)
}

func handleLongPress(recognizer: UIGestureRecognizer) {
    if recognizer.state == .Began {
        print("Duck! (location: \(recognizer.locationInView(nil))")
    }
}

func handleTap(recognizer: UIGestureRecognizer) {
    print("Jump! (location: \(recognizer.locationInView(nil))")
}

如果识别出长按 handleTap: 点击 不会 调用。只有当用户抬起手指的速度足够快时 handleTap: 才会被调用。否则 handleLongPress 将被调用。 handleLongPress 只有在长按持续时间过去后才会被调用。然后 handleLongPress 将被调用两次:持续时间结束时 ("Began") 和用户抬起手指后 ("Ended")。