Swift / Spritekit UILongPressGestureRecognizer似乎在按下时忽略所有其他手势

时间:2023-01-23 07:34:43

first off, i am new to swift development. i am learning and things are going mostly smoothly but i've come across this issue and can't seem to beat it down. i searched but there just isn't a lot of info out there. the few questions relating to this issue on SO aren't helpful either (perhaps i just don't know the proper terms to search for..?)

首先,我是快速发展的新手。我正在学习,事情进展顺利,但我遇到过这个问题,似乎无法击败它。我搜索但是那里没有很多信息。在SO上关于这个问题的几个问题也没有帮助(也许我只是不知道搜索的正确术语......?)

anyway.

i have set up my gesture recognizers for swipes, taps and long presses. they work fine. the problem comes when i do the long press. the long press is used to trigger player movement (technically it moves the background image(s)) it works, he animates and moves but while moving the system does not recognize any other gesture input. the moment i let go of the long press the other recognizers function as they should.

我已经为滑动,水龙头和长按设置了我的手势识别器。他们工作得很好。当我长按时出现问题。长按用于触发玩家移动(技术上它移动背景图像)它工作,他动画和移动但在移动系统时不识别任何其他手势输入。在我放弃长按的那一刻,其他识别器就像他们应该的那样起作用。

in my GameViewController i added

在我添加的GameViewController中

skView.multipleTouchEnabled = true

but it had no affect. it still doesn't seem to register the additional touch events while the long press is active. my code is below.

但它没有任何影响。在长按激活时,它似乎仍未注册其他触摸事件。我的代码如下。

class GameScene: SKScene {
override func didMoveToView(view: SKView) {

    // gesture recognizer
    // swipe up
    let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.handleSwipe(_:)))
    swipeUp.direction = .Up
    view.addGestureRecognizer(swipeUp)
    // swipe dn
    let swipeDn:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.handleSwipe(_:)))
    swipeDn.direction = .Down
    view.addGestureRecognizer(swipeDn)
    // swipe rt
    let swipeRt:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.handleSwipe(_:)))
    swipeRt.direction = .Right
    view.addGestureRecognizer(swipeRt)
    // swipe lt
    let swipeLt:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.handleSwipe(_:)))
    swipeLt.direction = .Left
    view.addGestureRecognizer(swipeLt)
    // tap
    let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(GameScene.handleTap(_:)))
    view.addGestureRecognizer(tapped)
    // long press
    let pressed:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(GameScene.handlePress(_:)))
    pressed.minimumPressDuration = 0.1
    view.addGestureRecognizer(pressed)
}


// swipes
func handleSwipe(sender: UISwipeGestureRecognizer) {
    switch sender.direction {
    case UISwipeGestureRecognizerDirection.Right:
        swipeRT = true
        print("swipe RIGHT")
    case UISwipeGestureRecognizerDirection.Left:
        swipeLT = true
        print("swipe LEFT")
    case UISwipeGestureRecognizerDirection.Up:
        swipeUP = true
        print("swipe UP")
    case UISwipeGestureRecognizerDirection.Down:
        swipeDN = true
        print("swipe DOWN")
    default:
        print("nothing to see here")
    }
}
// taps
func handleTap(sender: UITapGestureRecognizer) {
    print("tap")
}

func handlePress(sender: UILongPressGestureRecognizer) {
    if sender.state == .Began {
        theWAY()
    }
    if sender.state == .Ended {
        player.runAction(animIdle)
        moving = false
    }
}

i realize that there may be better/more efficient ways of achieving this. i'm 100% open to suggestions and critique. as i said, i'm new to this whole thing. thanks for any help and i apologize if this runs afoul of the whole "search before you post" thing. i did search, i promise.

我意识到可能有更好/更有效的方法来实现这一目标。我百分之百地接受建议和批评。正如我所说,我对这一切都是新手。感谢任何帮助,如果这与“你发布之前的搜索”事情发生冲突,我道歉。我保证会搜索。

1 个解决方案

#1


1  

You can use the UIGestureRecognizerDelegate method gestureRecognizer(_:shouldRecognizeSimultaneouslyWithGestureRecognizer:).

您可以使用UIGestureRecognizerDelegate方法gestureRecognizer(_:shouldRecognizeSimultaneouslyWithGestureRecognizer :)。

See this question and answer for reference.

请参阅此问题和答案以供参考。

#1


1  

You can use the UIGestureRecognizerDelegate method gestureRecognizer(_:shouldRecognizeSimultaneouslyWithGestureRecognizer:).

您可以使用UIGestureRecognizerDelegate方法gestureRecognizer(_:shouldRecognizeSimultaneouslyWithGestureRecognizer :)。

See this question and answer for reference.

请参阅此问题和答案以供参考。