Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement the UITapGestureRecognizer into my application

I am quite new to programming and Objective C. I was wondering how to make an app which has a blank screen and a timer that goes for one minute. You are meant to tap as fast as you can and as long as you can for. I was wondering how to implement the UITapGestureRecognizer into my code.

like image 823
objective_c_pro Avatar asked Jul 06 '12 04:07

objective_c_pro


People also ask

How to add tap gesture in ios?

Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

What is UITapGestureRecognizer?

UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.


2 Answers

This is a step by step guide on how to implement gesture recognizers in your class:

  1. Conform your class to UIGestureRecognizerDelegate protocol.

  2. Instantiate the gesture recognizer. For example, to instantiate a UITapGestureRecognizer, we will do:

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)]; 

    Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:

    - (void) handleTapFrom: (UITapGestureRecognizer *)recognizer {     //Code to handle the gesture } 

    The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like, UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded, etc.

  3. Set the desired properties on the instantiated gesture recognizer. For example, for a UITapGestureRecognizer, we can set the properties numberOfTapsRequired, and numberOfTouchesRequired.

  4. Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:

    [self.imageView addGestureRecognizer:tapGestureRecognizer]; 
  5. After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:

    tapGestureRecognizer.delegate = self; 

    Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won’t be called.

like image 189
Mathew Varghese Avatar answered Oct 07 '22 01:10

Mathew Varghese


Example in Swift:

import UIKit  class ViewController: UIViewController {      @IBOutlet var myUIImageView: UIImageView!      override func viewDidLoad() {         super.viewDidLoad()          let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "myUIImageViewTapped:")         singleTap.numberOfTapsRequired = 1         singleTap.numberOfTouchesRequired = 1         self.myUIImageView.addGestureRecognizer(singleTap)         self.myUIImageView.userInteractionEnabled = true     }      func myUIImageViewTapped(recognizer: UITapGestureRecognizer) {         if(recognizer.state == UIGestureRecognizerState.Ended){             println("myUIImageView has been tapped by the user.")         }     }  } 
like image 23
King-Wizard Avatar answered Oct 07 '22 01:10

King-Wizard