February 16th, 2018
Filed under: Game Development, iOS Development | Be the first to comment!
When you create a SpriteKit Xcode project, the GameScene.swift
file contains functions to handle touch and/or mouse events, depending on the type of SpriteKit project you create. You can also use swipe gestures in SpriteKit games. Supporting swipe gestures requires you to perform two tasks.
The first task is to create a swipe gesture recognizer. The swipe gesture recognizer takes two arguments: a target and a selector (function) to call when someone performs the gesture. Set the swipe gesture recognizer’s direction and add it to the view. The following code demonstrates how to create a swipe gesture recognizer for swiping right:
override func didMove(to view: SKView) {
let swipeRight = UISwipeGestureRecognizer(target: self,
action: #selector(GameScene.swipeRight(sender:)))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
}
The second task is to write a function to handle the swipe.
@objc func swipeRight(sender: UISwipeGestureRecognizer) {
// Handle the swipe
}
The functions to handle the swipes need the @objc
at the start because they’re selectors that access the Objective-C runtime.