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.
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.
Step 3
Now, open the ViewController.Swift file and write the following code.
- let swipeRight = UISwipeGestureRecognizer(target: self, action: "respond:")
- swipeRight.direction = .Right
- view.addGestureRecognizer(swipeRight)
-
- let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respond:")
- swipeLeft.direction = .Left
- view.addGestureRecognizer(swipeLeft)
-
- let swipeUp = UISwipeGestureRecognizer(target: self, action: "respond:")
- swipeUp.direction = .Up
- view.addGestureRecognizer(swipeUp)
-
- let swipeDown = UISwipeGestureRecognizer(target: self, action: "respond:")
- swipeDown.direction = .Down
- 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.
- func respond(gesture: UISwipeGestureRecognizer)
- {
- if let swipeGesture = gesture as ? UISwipeGestureRecognizer
- {
- switch swipeGesture.direction {
- case UISwipeGestureRecognizerDirection.Right:
- lblDirection.text = "Right"
- case UISwipeGestureRecognizerDirection.Left:
- lblDirection.text = "Left"
- case UISwipeGestureRecognizerDirection.Up:
- lblDirection.text = "UP"
- case UISwipeGestureRecognizerDirection.Down:
- lblDirection.text = "Down"
- default:
- break
- }
- }
- }
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.