touchesBegan 与 UITapGestureRecognizer

touchesBegan Vs UITapGestureRecognizer

所以我正在使用 swift 制作 spritekit 游戏。我想知道哪种手势最适合使用?

目前我有一个 override func touchesBegan 处理所有点击和一个 UILongPressGestureRecognizer 处理长 taps/holds。

所以你知道,点击按钮并跳跃英雄。长按让英雄低头。

出于某种原因,我的 longPress 功能并不总是被调用(有时您可以按住 10 次然后它停止被调用(无法识别),其他时候是 5 次,它会有所不同),这导致昨天花了一整天尝试新事物和调查,这让我想到了这个问题。

使用 touchesBegan 还是将我所有的触摸调用移至由 UITapGestureRecognizer 处理的新函数更好?

我确实将所有内容从 touchesBegan 移到了 UITapGestureRecognizer,但它看起来很慢。但我可能执行错了?

这是 recognisers 的设置方式:

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

    let longTapRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("handleLongPress:"))
    longTapRecognizer.minimumPressDuration = 0.2
    view!.addGestureRecognizer(longTapRecognizer)

}

这些是处理手势的函数:

func handleTap(recognizer: UIGestureRecognizer) {
    //currently all handled in touchesBegan
}

func handleLongPress(recognizer: UIGestureRecognizer) {
    print("1 --------> longPress Called.... ", recognizer.state.rawValue, gameState)
    if gameState == .Play {
       //do stuff
       //duck Hero
    } else {
       //switch gameState
    }
}

这是处理 touches/taps:

的函数
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

for touch in touches {
     let location = touch.locationInNode(self)

     //do stuff

     switch gameState {
        case .MainMenu:
        break
        ... //more states
        }
   }
   super.touchesBegan(touches, withEvent: event)
}

如果我将所有内容从 touchesBegans 移动到 tapRecogniser(上面的空函数),我也必须实现它,以转换触摸位置坐标:

func handleTap(recognizer: UIGestureRecognizer) {
    let location = convertPointFromView(CGPoint(x: recognizer.locationInView(nil).x, y: recognizer.locationInView(nil).y))
    print("Converted Coords: ", location)

    //then do all touchesBegan stuff
 }

我都试过了,但后者似乎真的很慢而且迟钝。也许我忘了实施推荐的东西?

似乎我的长按手势并不总是被调用,这之间会不会有一些冲突?

因此,如果您按住红色方块两秒钟,您会收到一条消息,当您松开时,消息就会消失。您可能必须在其中添加一些布尔值,以确保您的角色在按住 2 秒按钮后不会每帧重复一些动作。这应该足以让你开始充满希望

import SpriteKit

class GameScene: SKScene {

    // time values
    var delta = NSTimeInterval(0)
    var last_update_time = NSTimeInterval(0)

    var longTouchTimer = NSTimeInterval(0)
    var touched = false
    var testLabel = SKLabelNode(text: "you have touched for awhile now bro")

    let touchSprite = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(100, 100))

    override func didMoveToView(view: SKView) {
        touchSprite.position = CGPointMake(self.size.width/2, self.size.height/2)
        addChild(touchSprite)

        testLabel.hidden = true
        testLabel.position = touchSprite.position
        testLabel.position.y += 100
        testLabel.fontSize = 20
        addChild(testLabel)
    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            if touchSprite.containsPoint(location) {
                touched = true
            }
        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            if !touchSprite.containsPoint(location) {
                touched = false
                longTouchTimer = 0
            }
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        touched = false
        longTouchTimer = 0
    }

    override func update(currentTime: NSTimeInterval) {
        if last_update_time == 0.0 {
            delta = 0
        } else {
            delta = currentTime - last_update_time
        }

        last_update_time = currentTime

        if touched {
            longTouchTimer += delta
        }

        if longTouchTimer >= 2 {
            testLabel.hidden = false
        } else {
            testLabel.hidden = true
        }
    }
}