UIGestureRecognizer in Swift

In this short tutorial, we will see the steps required for implementing UIGestureRecognizer in Swift programming language. Let us take the previous Stop Watch demo code and implement tap, double tap and swipe gestures. You can download source code for Stop Watch from here.

201407231513.jpg

The following features will be implemented using the UIGestureRecognizer.

  • Tap – Starts the timer.
  • Double Tap – Stops the timer
  • Swipe (from left to right) – Resets the timer.

Remove the buttons

Since we are replacing the buttons with gestures, these buttons can be removed from the View Controller. Navigate to Main.storyboard and select the Start and Stop buttons and delete them. Make sure the timer label is centre aligned both vertically and horizontally. Then click Reset to Suggested Constraints under Resolve Auto Layout Issues option.

201407231527.jpg

Write Gestures code

Click SWViewController.swift file in the Project navigator and navigate to viewDidLoad function. Then add the following code to viewDidLoad method

  override func viewDidLoad() {

super.viewDidLoad()

  

let aSelector : Selector = “start:”

let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)

tapGesture.numberOfTapsRequired = 1

view.addGestureRecognizer(tapGesture)

  

}

First this creates a constant to hold the selector argument which will be used when creating UITapGestureRecognizer. We are reusing the function that was used for the Start button.

Then we specify the numberOfTapsRequried and add the gesture as part of the view. Now repeat this for the stop button as well but for double tap.

  let bSelector : Selector = “stop:”

let doubleTapGesture = UITapGestureRecognizer(target: self, action: bSelector)

doubleTapGesture.numberOfTapsRequired = 2

view.addGestureRecognizer(doubleTapGesture)

  

tapGesture.requireGestureRecognizerToFail(doubleTapGesture)

The only difference is the number of taps required is specified as 2 and we are specifying that the single tap function will be called only when gesture is not double tap. Now if you try to run this project in the simulator, you should be able start and stop the timer using tap and double tap gestures.

Adding Swipe Gesture

When the user does a swipe from left to right, the values in the timer label must be set to 00:00:00. We can do this by using the UISwipeGestureRecognizer. Let us first the add function that resets the timer label.

  @IBAction func reset(sender: AnyObject) {

displayTimeLabel.text = “00:00:00”

}

Then add the following code to viewDidLoad method

  let cSelector : Selector = “reset:”

let rightSwipe = UISwipeGestureRecognizer(target: self, action: cSelector)

rightSwipe.direction = UISwipeGestureRecognizerDirection.Right

view.addGestureRecognizer(rightSwipe)

When a swipe from left to right is done the reset function is called. The viewDidLoad function with all the Gesture Recognizer should look as shown below.

  override func viewDidLoad() {

super.viewDidLoad()

  

let aSelector : Selector = “start:”

let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)

tapGesture.numberOfTapsRequired = 1

view.addGestureRecognizer(tapGesture)

  

let bSelector : Selector = “stop:”

let doubleTapGesture = UITapGestureRecognizer(target: self, action: bSelector)

doubleTapGesture.numberOfTapsRequired = 2

view.addGestureRecognizer(doubleTapGesture)

  

tapGesture.requireGestureRecognizerToFail(doubleTapGesture)

  

let cSelector : Selector = “reset:”

let rightSwipe = UISwipeGestureRecognizer(target: self, action: cSelector)

rightSwipe.direction = UISwipeGestureRecognizerDirection.Right

view.addGestureRecognizer(rightSwipe)

}

Try running the project in simulator and check out tap, double tap and swipe gestures.

201407231555.jpg

Source code

In Category: Develop, ios, Xcode

Ravi Shankar

A polyglot software developer and now exploring Swift and iOS development. If you would like to learn from me then check out services page.

Show 9 Comments
  • Dominic August 13, 2014, 11:14 pm

    Thank you for the selector syntax in Swift!

  • Mr.Singh August 21, 2014, 11:24 am

    Thanks for the tutorial. I’m on Xcode 6 Beta 5 and for some reason it’s not supporting the “numberOfTapsRequired” method. I don’t see an option there. Rather what i get is “tapGesture.numberOfTouches()” Do i need to import any class?

    • rshankar August 21, 2014, 11:31 am

      Can you please check whether the variable is a instance of UIGestureRecognizer or UITapGestureRecognizer? It needs to be UITapGestureRecognizer

  • Jesper September 9, 2014, 1:57 pm

    Thanks for the tutorial!! Is it possible to make one button (start) to start the clock, and then the same button to stop the clock, with one click? In in the meantime the text inside the button, change from start to stop to start …. etc.?

  • Jon September 13, 2014, 12:36 am

    Thanks for this very clear example!! Best example I’ve seen.

  • Ole October 20, 2014, 3:17 pm

    Great. Just what I needed for my little project.
    Thank you!

  • Glade December 13, 2014, 11:45 pm

    When I tap to start it will reset the timer, not continue counting. The button example also has the same problem. Does anyone know how to fix this?

  • Alex March 6, 2015, 4:21 pm

    Great tutorial. Can’t figure out how to make tap on uiwebview work, do you know what the trick? Tx

  • Alex January 28, 2016, 12:41 pm

    Thanks for showing the creation of a selector. This hadn’t occurred to me and I’ve always hated creating selectors as strings directly into the parameters, too much opportunity for misspellings, etc. I came to this page searching for a UITapGestureRecognizer with a block, hoping Apple had added this, like they did for NSNotifications.

Leave a Comment