我正在尝试检测视图上的双击,但是当双击时,第一次点按会触发TouchesBegan上的操作,因此,在检测到双击之前,始终会首先检测到单击。
如何才能检测到双击?
我无法使用OS 3.x手势,因为我必须使其与旧操作系统版本兼容。
感谢
答案 0 :(得分:14)
来自scrollViewSuite示例代码的tapZoom示例的一些摘录:
首先,触摸结束后启动的功能:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 1) {
[self performSelector: @selector(handleSingleTap)
withObject: nil
afterDelay: 0.35]; // after 0.35s we call it a single tap
} else if([touch tapCount] == 2) {
[self handleDoubleTap];
}
}
第二:如果在超时期间发生新的触摸,则拦截消息:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[NSObject cancelPreviousPerformRequestsWithTarget: self
selector: @selector(handleSingleTap)
object: nil];
}
和这里:(scrollView套件) http://developer.apple.com/iphone/library/samplecode/ScrollViewSuite/Introduction/Intro.html
答案 1 :(得分:6)
编辑:我错过了你说你不能使用3.x手势的观点,所以这是对你的问题的无效答案,但我离开它以防万一可以使用3.x手势的人可以从中受益
您可以创建两个手势识别器,一个用于单击,一个用于双击:
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)];
singleTapGesture.cancelsTouchesInView = NO;
singleTapGesture.delaysTouchesEnded = NO;
singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap
singleTapGesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTapGesture];
[singleTapGesture release];
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesTwo:)];
doubleTapGesture.cancelsTouchesInView = NO;
doubleTapGesture.delaysTouchesEnded = NO;
doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap
doubleTapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTapGesture];
[doubleTapGesture release];
然后,来了一拳:
[singleTapGesture requireGestureRecognizerToFail : doubleTapGesture];
最后一行,只有在双击失败时,您的单击处理程序才能工作。因此,您可以在应用程序中同时进行单击和双击。
答案 2 :(得分:2)
你在看tapCount吗?例如:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (touch.tapCount == 2) {
//double-tap action here
}
}