Batredd Batredd - 2 months ago 19
Swift Question

Distinguish Swipe from Touch in Spritekit - Swift 3

I am currently working on an arcade app where the user taps for the sprite to jump over an obstacle and swipes down for it to slide under an obstacle. My problem is that when I begin a swipe the touchesBegan function is called so the sprite jumps instead of sliding. Is there a way to distinguish these two?

Answer

You can use a gestures state to fine tune user interaction. Gestures are coordinated, so shouldn't interfere with each other.

func handlePanFrom(recognizer: UIPanGestureRecognizer) {
    if recognizer.state != .changed {
        return
    }

    // Handle pan here
}

func handleTapFrom(recognizer: UITapGestureRecognizer) {
    if recognizer.state != .ended {
        return
    }

    // Handle tap here
}
Comments