Swift 游戏 - 点击并点击 + 保持手势?

Rea*_*ion 1 cocoa-touch uigesturerecognizer sprite-kit swift

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

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

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

所以基本的伪代码:

if tapped
    heroJump()
else if tappedAndHeld
    heroDuck()
Run Code Online (Sandbox Code Playgroud)

我有一个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)
}
Run Code Online (Sandbox Code Playgroud)

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

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

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)
}
Run Code Online (Sandbox Code Playgroud)

这些是手势调用的功能:

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()
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能把这两件事结合起来?我可以添加一种方法来确定它是在我的 touchBegins 事件中被点击还是保持,或者我可以废弃该方法并仅使用上述两个功能吗?

如果使用后者,获取位置的众多问题之一是什么?

或者,也许我认为这是完全错误的,并且在 swift/spritekit 中有一个简单和/或内置的方法?

谢谢。

joe*_*ern 6

您只需要UITapGestureRecognizerUILongPressRecognizer。您不需要对touchesBeganand做任何事情touchesEnded,因为手势识别器会分析触摸本身。

要获取触摸的位置,您可以调用locationInView手势识别器来获取手势的位置,或者locationOfTouch如果您正在使用多点触控手势并需要每次触摸的位置。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))")
}
Run Code Online (Sandbox Code Playgroud)

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