Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

7. Implementing Gesture Recognizers > 7.5. Detecting Tap Gestures

7.5. Detecting Tap Gestures

Problem

You want to be able to detect when users tap on a view.

Solution

Create an instance of the UITapGestureRecognizer class and add it to the target view, using the addGestureRecognizer: instance method of the UIView class. Let’s have a look at the definition of the view controller (the .h file):

#import <UIKit/UIKit.h>

@interface Detecting_Tap_GesturesViewController : UIViewController

@property (nonatomic, strong) 
  UITapGestureRecognizer *tapGestureRecognizer;

@end

The implementation of the viewDidLoad instance method of the view controller is as follows:

- (void)viewDidLoad {
  [super viewDidLoad];
  
  self.view.backgroundColor = [UIColor whiteColor];
  
  /* Create the Tap Gesture Recognizer */
  self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] 
                               initWithTarget:self
                               action:@selector(handleTaps:)];
  
  /* The number of fingers that must be on the screen */
  self.tapGestureRecognizer.numberOfTouchesRequired = 2;
  
  /* The total number of taps to be performed before the 
   gesture is recognized */
  self.tapGestureRecognizer.numberOfTapsRequired = 3;
  
  /* Add this gesture recognizer to the view */
  [self.view addGestureRecognizer:self.tapGestureRecognizer];
  
}

- (void) viewDidUnload{
  [super viewDidUnload];....

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint