Vladimir Stazhilov
Vladimir Stazhilov

Reputation: 1954

iPhone SDK, how to make a button respond to a double click?

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

How should I do that?

Upvotes: 1

Views: 1001

Answers (4)

Tornado
Tornado

Reputation: 1087

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

Upvotes: 2

Mahir
Mahir

Reputation: 1684

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
{

}

Upvotes: 2

Kheldar
Kheldar

Reputation: 5389

Try this.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [[event allTouches] anyObject];
     if (touch.tapCount == 2) {
     }
}

Upvotes: 0

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73688

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];

Upvotes: 2

Related Questions