Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Gesture Recognizers with Sprite Kit

Gesture Recognizers with Sprite Kit
October 3, 2013 Justin

Everyone loves gestures right? Long ago Apple realized it would be convenient to have an easy way to detect things like swiping (in any direction), pinching, rotating, panning, etc. So lets take a quick look at using these in the Sprite Kit world. And I’ll point out one big problem, that was easily solvable, but you might run into this issue too.

First off, let’s declare some gesture recognizers in our header file for the scene class and tell the class it will be our delegate (in my project the exact filename  is CSLevel.h)….

Perfect. Now that we’ve declared some recognizers, we can use them in these two methods, didMoveToView.  and willMoveFromView. (Queue a Starship Troopers style “Do You Want To Know More” style pop-up here). If you want to know more, search your official documentation’s SKScene Class Reference under Detecting Scene Presentation Changes. Both methods are intended to be overwritten in a subclass (thats where we come in). So the aptly named, didMoveToView method will be called immediately after a scene is presented by a view and willMoveFromView will be called  immediately before a scene is removed from a view. Both of which bring along a parameter of 🙁SKView* )view.   So take a look….

Great work. One important thing you don’t want to overlook here is the parameter view.  Without that, you won’t have an easy way to access the SKView which is where we want to add our gesture recognizers too. Prior to realizing this was the way of doing it, I tried some other methods and it didn’t go so well (I was messing with the View Controller though).

Both of those gestures recognizers need accompanying methods to handle what occurs if a swipe-right or rotation is detected. We gave them names already ofhandleSwipeRight: and handleRotation: so be sure to include them with therecognizer parameter passed in.  Why pass in a reference to the recognizer that actually caused the method to run. Well one reason is to test the state of the recognizer, for example below, we are seeing if recognizer.state is equal to UIGestureRecognizerStateEnded. This lets us isolate our code to only run once the rotation has stopped. Otherwise this handleRotation: method could get called multiple times during one rotation.

Another reason to bring in your recognizer as a parameter is to test things like the number of touches involved in the triggering event. For example, our handleSwipeRightcould look like this…

Now here’s the BIG RED ALERT.  The “gotcha”. I noticed when transitioning scenes (for example, from my level scene to my start menu) that these gestures hung around. I assumed that by dumping the scene it would cleanse my gestures, but not so. And in hindsight it makes sense that they stuck around. But as soon as my level was gone, the gestures had no corresponding handlers (the handleSwipeRight: and handleRotation:methods couldn’t be found) and the app crashed mysteriously.

So in your willMoveFromView:  method we need to remove the gestures recognizers…

You can’t really test that unless you transition to another scene. So if you’ve got another scene class, give it a shot with this method (my other scene is called CSStartMenu )…

Alrighty folks. Hope that helps! Don’t forget to look for more Sprite Kit tutorials (in video form) at CartoonSmart.com


Justin Dike is the owner and lead developer at CartoonSmart.com. He's taught hundreds of hours of game development video tutorials, covering everything from coding to art. These days he's working mostly on Swift and Sprite Kit tutorials, which often lead into highly polished iOS / tvOS Starter Kits, which require no programming at all, but are capable of making nearly any type game! Yeah, you read that write. You can also find Justin at the official CartoonSmart Podcast


  1. Dave 6 years ago

    Hi Justin, Your big red alert help me resolve this problem, was suspicious recognizers didn’t get cleaned up when my app was crashing.

    Now Im having a related problem, the recognizers don’t seem to remove quickly enough. I have tap recognisers in 2 scenes, If player does a tap in scene 1 and keeps tapping (e.g. kids) when the scene transitions to scene 2 (with a central button) the app still crashes. Leave it a second or more and its fine. Any thoughts?

    • Author
      Justin 6 years ago

      I feel like I’ve seen that before. It might not be that the gesture is crashing because it isn’t getting removed quick enough though. It could be that function being called from gesture2 is getting called in scene 1 or vice versa. If the gestures call the same function name, you might want to change them to see if that makes a difference. Also just try enclosing your code in those functions in something like…

      -(void)transitionScenes {

      if (transitionInProgress == NO){
      //do transition
      transitionInProgress = YES



      Good luck!

Leave a reply

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com