Gesture Recognizer Examples with Sprite Kit and Swift 3
You can copy all the code below into your GameScene.swift file and test out 4 swipe gestures, 2 tap gestures, and a rotation gesture…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import SpriteKit import GameplayKit class GameScene: SKScene { //First we declare all of our Gestures... //swipes let swipeRightRec = UISwipeGestureRecognizer() let swipeLeftRec = UISwipeGestureRecognizer() let swipeUpRec = UISwipeGestureRecognizer() let swipeDownRec = UISwipeGestureRecognizer() //rotate let rotateRec = UIRotationGestureRecognizer() //taps let tapRec = UITapGestureRecognizer() let tapRec2 = UITapGestureRecognizer() override func didMove(to view: SKView) { swipeRightRec.addTarget(self, action: #selector(GameScene.swipedRight) ) swipeRightRec.direction = .right self.view!.addGestureRecognizer(swipeRightRec) swipeLeftRec.addTarget(self, action: #selector(GameScene.swipedLeft) ) swipeLeftRec.direction = .left self.view!.addGestureRecognizer(swipeLeftRec) swipeUpRec.addTarget(self, action: #selector(GameScene.swipedUp) ) swipeUpRec.direction = .up self.view!.addGestureRecognizer(swipeUpRec) swipeDownRec.addTarget(self, action: #selector(GameScene.swipedDown) ) swipeDownRec.direction = .down self.view!.addGestureRecognizer(swipeDownRec) //notice the function this calls has (_:) after it because we are passing in info about the gesture itself (the sender) rotateRec.addTarget(self, action: #selector (GameScene.rotatedView (_:) )) self.view!.addGestureRecognizer(rotateRec) // again notice (_:), we'll need this to find out where the tap occurred. tapRec.addTarget(self, action:#selector(GameScene.tappedView(_:) )) tapRec.numberOfTouchesRequired = 1 tapRec.numberOfTapsRequired = 1 self.view!.addGestureRecognizer(tapRec) tapRec2.addTarget(self, action:#selector(GameScene.tappedView2(_:) )) tapRec2.numberOfTouchesRequired = 1 tapRec2.numberOfTapsRequired = 2 //2 taps instead of 1 this time self.view!.addGestureRecognizer(tapRec2) } //the functions that get called when swiping... func swipedRight() { print("Right") } func swipedLeft() { print("Left") } func swipedUp() { print("Up") } func swipedDown() { print("Down") } // what gets called when there's a single tap... //notice the sender is a parameter. This is why we added (_:) that part to the selector earlier func tappedView(_ sender:UITapGestureRecognizer) { let point:CGPoint = sender.location(in: self.view) print("Single tap") print(point) } // what gets called when there's a double tap... //notice the sender is a parameter. This is why we added (_:) that part to the selector earlier func tappedView2(_ sender:UITapGestureRecognizer) { let point:CGPoint = sender.location(in: self.view) print("Double tap") print(point) } //what gets called when there's a rotation gesture //notice the sender is a parameter. This is why we added (_:) that part to the selector earlier func rotatedView(_ sender:UIRotationGestureRecognizer) { if (sender.state == .began) { print("rotation began") } if (sender.state == .changed) { print("rotation changed") //you could easily make any sprite's rotation equal this amount like so... //thePlayer.zRotation = -sender.rotation //convert rotation to degrees... let rotateAmount = Measurement(value: Double(sender.rotation), unit: UnitAngle.radians).converted(to: .degrees).value print("\(rotateAmount) degreess" ) } if (sender.state == .ended) { print("rotation ended") } } func removeAllGestures(){ //if you need to remove all gesture recognizers with Swift you can do this.... for gesture in (self.view?.gestureRecognizers)! { self.view?.removeGestureRecognizer(gesture) } //this is good to do before a SKScene transitions to another SKScene. } func removeAGesture() { //To remove a single gesture you can use... self.view?.removeGestureRecognizer(swipeUpRec) } } |