• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips
You are here: Home / Develop / UIGestureRecognizer in Swift

UIGestureRecognizer in Swift

July 23, 2014 By Ravi Shankar 9 Comments

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

Filed Under: Develop, ios, Xcode Tagged With: Swift, UIGestureRecognizer, UISwipeGestureRecognizer, UITapGestureRecognizer, Xcode

Reader Interactions

Comments

  1. Dominic says

    August 13, 2014 at 11:14 pm

    Thank you for the selector syntax in Swift!

    Reply
  2. Mr.Singh says

    August 21, 2014 at 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?

    Reply
    • rshankar says

      August 21, 2014 at 11:31 am

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

      Reply
  3. Jesper says

    September 9, 2014 at 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.?

    Reply
  4. Jon says

    September 13, 2014 at 12:36 am

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

    Reply
  5. Ole says

    October 20, 2014 at 3:17 pm

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

    Reply
  6. Glade says

    December 13, 2014 at 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?

    Reply
  7. Alex says

    March 6, 2015 at 4:21 pm

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

    Reply
  8. Alex says

    January 28, 2016 at 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.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy