views:

115

answers:

2

Im talking about two separate touches on the screen with the same finger

I think i have the coding right for this. but it is almost impossible for me to actually generate a double tap on my iPad. Is it possible to increase the time interval between a single and double tap, so it is easier to trigger. I do a double tap very fast and it captures it as two single clicks. only sometimes am i lucky enough to trigger a double tap. I place my subclass of UIButton item into a scrollview.

Anyway my subclass of UIButton implements:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{

 UITouch *touch = [[event allTouches] anyObject];

 NSLog(@"Touch count:%d",touch.tapCount);

 if (touch.tapCount == 1) 
 {
  //Do things for one touch
 }
 else if (touch.tapCount == 2) 
 {
  //Do things for double touch
 }
}

This is capturing the event. and that is why i think my code is right, i just couldnt find anything that has to do with UIEvent and what determines how many touches happen. I tested this same thing in a different UIView and it worked exactly as expected.

A: 

Your code is detecting two fingers in the view at the same time, not a rapid sequence of on finger (double tap).

If you want to detect a double tap, the easiest way is to use a Gesture Recoginizer. Wherever do setup of your sublass, add this code:

UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerDoubleTap:)];
singleFingerDoubleTap.numberOfTouchesRequired = 1;
singleFingerDoubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];

And implement a method to hande the double tap:

-(void)handleSingleFingerDoubleTap:(id)sender {
  //Do stuff
}
Brad Smith
Are you sure, cause i tried this in a class i have that is just a UIView and it captured a 'double' tap just as i expected. but either way,any ideas on how to catch a double tap?
Kyle Martin
I just edited the answer to give you an example
Brad Smith
The only caveat is that gesture recognizers are only available for iOS 3.2 and greater, so this code wont work for 3.0 and 3.1 devices.
Brad Smith
Older devices shouldnt be a problem for me
Kyle Martin
I just reread your first comment, and I guess I might have been confused about what you meant by double tap. (two - fingers vs two rapid taps) Either way the gesture recognizer should handle wither case, be setting the numberOfTouchesRequired property to the number of fingers on the screen at the same time, and the numberOfTapsRequired property to the number of sequential taps.
Brad Smith
This does exactly the same thing as my original code. Maybe the problem lies in the face that my UIButton is in a scrollview.
Kyle Martin
A: 

you not calling [super touchesBegan:touches withEvent:event]

GameBit
Yeah, i know, but still my log statement should show a double tap. Which is really hard to do. ill put that in and see my luck.
Kyle Martin
That didnt help at all
Kyle Martin