1

In my iphone app, I've got buttons and I should make them respond to a double click.

How should I do that?

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Vladimir Stazhilov
  • 1,956
  • 4
  • 31
  • 63

4 Answers4

2

You need to use gestures, Try this -

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    [doubleTap setNumberOfTapsRequired:2];
    [doubleTap setNumberOfTouchesRequired:1];
    [yourView addGestureRecognizer:doubleTap];
    [doubleTap release];
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
2

You have to use UIGestures

In the initWithFrame (or initWithCoder if you're using Interface Builder)

   UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    doubleTap.numberOfTapsRequired = 2;
    [self addGestureRecognizer:doubleTap];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    singleTap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:singleTap];
    [singleTap requireGestureRecognizerToFail:doubleTap];

    [doubleTap release];
    [singleTap release];

Then add the methods

- (void) doubleTap: (UITapGestureRecognizer *) sender
{

     do whatever
}

- (void) singleTap:(UITapGestureRecognizer *)sender
{

}
Mahir
  • 1,684
  • 5
  • 31
  • 59
2

Try adding following to button defination :-

[button addTarget:self action:@selector(buttontappedtwice)     forControlEvents:UIControlEventTouchDownRepeat];

where buttontappedtwice is a method which will be called when u tap this particular button twice... Cheers

Tornado
  • 1,069
  • 12
  • 28
  • And read here for some useful implementation detail in your selector method - http://stackoverflow.com/questions/4109582/double-touch-on-uibutton. – Perception Aug 10 '11 at 11:00
0

Try this.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [[event allTouches] anyObject];
     if (touch.tapCount == 2) {
     }
}
Kheldar
  • 5,361
  • 3
  • 34
  • 63