Gesture Recognition - Recognizing Swipes

Introduction

In this article, we will implement the Gesture function in iOS app development, using Swift. Gesture is basically the custom movement and rotation of an arrow in our touch screen. We will see how to implement this in our iOS application. We will take one label and only move our arrow; you will see that the label text has changed in order to indicate the position of our arrow where we move it.

Step 1

Open the Xcode Editor and create a “Single View Application"; choose the application type as universal; and then, create a project. Then, go to the Main Storyboard file. In the object library, drag the label, and drop it into the ViewController.

Single View Application

Step 2

Now, open the editor in Assistant mode. You will see two circles at the top right side, which overlap each other. Click on that button and now your editor assistant mode is open. In the left side, open the Main storyboard and on the other side,  open the ViewController.Swift file. Now, make the connection between label and swift file and give it a name, such as lblDirection.

lblDirection

Step 3

Now, open the ViewController.Swift file and write the following code.

  1. let swipeRight = UISwipeGestureRecognizer(target: self, action: "respond:")  
  2. swipeRight.direction = .Right  
  3. view.addGestureRecognizer(swipeRight)  
  4.   
  5. let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respond:")  
  6. swipeLeft.direction = .Left  
  7. view.addGestureRecognizer(swipeLeft)  
  8.   
  9. let swipeUp = UISwipeGestureRecognizer(target: self, action: "respond:")  
  10. swipeUp.direction = .Up  
  11. view.addGestureRecognizer(swipeUp)  
  12.   
  13. let swipeDown = UISwipeGestureRecognizer(target: self, action: "respond:")  
  14. swipeDown.direction = .Down  
  15. view.addGestureRecognizer(swipeDown)  
Add this code in viewDidLoad method. In the first line of code, we make a constant variable of swipeRight. Then, we set the action and target of Swipe Gesture. In the second line, we give the direction that when we move our arrow on the right side, the application will add the gesture in View. The next three methods - swipeLeft, swipeUp and swipeDown are same as the right swipe.

Step 4

Now, we make the function outside the ViewDidLoad method. Basically, this function will perform the work, that is, when user moves the arrow upward, it will print the text in label.
  1. func respond(gesture: UISwipeGestureRecognizer)   
  2. {  
  3.     if let swipeGesture = gesture as ? UISwipeGestureRecognizer   
  4.     {  
  5.         switch swipeGesture.direction {  
  6.             case UISwipeGestureRecognizerDirection.Right:  
  7.                 lblDirection.text = "Right"  
  8.             case UISwipeGestureRecognizerDirection.Left:  
  9.                 lblDirection.text = "Left"  
  10.             case UISwipeGestureRecognizerDirection.Up:  
  11.                 lblDirection.text = "UP"  
  12.             case UISwipeGestureRecognizerDirection.Down:  
  13.                 lblDirection.text = "Down"  
  14.             default:  
  15.                 break  
  16.         }  
  17.     }  
  18. }   
Now, first we make a function named respond; pass the parameter gesture; and give the condition inside the function that if our arrow moves, then go to the switch condition and print the position of where the user moved his arrow, i.e., left, right, up, and dow, to the side.

Now, run the app. You will see that wherever you move the cursor, it prints the text in a label. Remember, it can’t capture the arrow sign.

position position position  


Similar Articles