Tuesday, March 04, 2025

Handling Gesture Recognizers and TouchesBegan in SpriteKit

Are you working on a SpriteKit game and trying to use both gesture recognizers and touchesBegan? I spent more time than I expected figuring this out. After asking AIs and not getting clear answers, I ended up checking the friendly manual myself.

Thought I’d write a quick blog post about it—maybe it’ll help someone else searching for this later.

The issue is that touchesBegan happens before the gesture recognizer. There’s an easy fix: set delaysTouchesBegan to true. Here’s an example:

override func didMove(to view: SKView) {
        let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanFrom))
        panGestureRecognizer.delegate = self
        panGestureRecognizer.delaysTouchesBegan = true
        self.view!.addGestureRecognizer(panGestureRecognizer)
} 

Why was it tricky to find? I think it’s because SpriteKit doesn’t get much attention these days, so there’s not a lot of discussion about it online. Hopefully this saves you some time!

No comments:

Post a Comment