Lessons and Playgrounds Code Snippets for Swift 3, Sprite Kit, and Xcode 8

Swift 3 Gesture Recognizers

The following code should be tested in an actual Xcode project (not a Playgrounds file)

Declare these variables in your class…

Swipe Gesture Recognizers…

Next in any function in your class you can add the gestures to the view. For game developers using Sprite Kit, you’ll most likely do this in your GameScene’s didMove(to view:SKView ) function. Lets start with the Swipe Gesture Recognizers…

We will get an error until we write the swipedRight, swipedLeft, swipedUp and swipedDown functions in the GameScene class (or whatever class name you’re writing these in). Before you write those functions, just note the .direction property obviously specifies the direction of the swipe. And each gesture is added to self.view!  Your functions that will run for each swipe can be as simple as the one below…

Tap Gesture Recognizers…

Taps can have a few more notable properties. For example…

The numberOfTouchesRequired determines how many fingers need to be touching the screen to call the tappedView function.  Likewise the numberOfTapsRequired specifies how many quick taps have to be made in order for the function to be called. So you can combine these properties together to make some pretty crazy tap requirements. Like 3 fingers must tap 4 times. Most likely, you’ll never setup something that specific.  A

Rotation Gesture Recognizers…

Setting up the rotation gesture takes the least amount of work…

But notice the rotatedView function includes this after it…  (_:)  That means we’ll be passing information about the sender (the gesture) into the rotatedView function. Which looks like this…

 

Ready for hundreds of hours of iOS and tvOS video tutorials, Starter Kits and a lot more Swift fun?

Visit CartoonSmart.com