33

I am developing an application where I have multiple controls on view but I want to enable them when user double tap the view

You can take the example of double click but in device I want to catch the event when their is double tap.

Azhar
  • 20,500
  • 38
  • 146
  • 211

2 Answers2

78

You need to add an UITapGestureRecognizer to the view which you want to be tapped.

Like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateRecognized) {
        // handling code
    }
}
xuzhe
  • 5,100
  • 22
  • 28
  • 6
    in case of the existence of multiple gesture recognizers, you can ensure the double tap one of "high priority" by using: [self.view.tapGestureRecognizer requireGestureRecognizerToFail:self.doubleTapGestureRecognizer]; – Jerry Tian Nov 08 '12 at 09:25
  • 2
    don't forget to add the to your class private interface ... @interface IBMYourClassName () – MB_iOSDeveloper Sep 02 '14 at 09:01
8

Add a UITapGestureRecognizer to the view, with numberOfTapsRequired = 2.

Morrowless
  • 6,856
  • 11
  • 51
  • 81
  • is UITapGestureRecognizer a control? Its not in controls Library – Azhar Sep 06 '11 at 07:16
  • It's a UIGestureRecognizer subclass. You need to write it in code. – Benjamin Mayo Sep 06 '11 at 07:18
  • I write this code with selector and UIAlert but it doesnot work - (void)viewDidLoad { UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; tapGesture.numberOfTapsRequired = 2; [tapGesture release]; } – Azhar Sep 06 '11 at 07:42
  • Did you add it to the view using addGestureRecognizer: ? – Morrowless Sep 06 '11 at 08:09