当用户触摸视图时,我想检测JUST双击/单击。
我做了这样的事情:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint prevLoc = [touch ]
if(touch.tapCount == 2)
NSLog(@"tapCount 2");
else if(touch.tapCount == 1)
NSLog(@"tapCount 1");
}
但它总是在2次点击之前检测到1次点击。我怎样才能检测到1/2水龙头?
答案 0 :(得分:3)
谢谢你的帮助。我也找到了这样的方式:
-(void)handleSingleTap
{
NSLog(@"tapCount 1");
}
-(void)handleDoubleTap
{
NSLog(@"tapCount 2");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger numTaps = [[touches anyObject] tapCount];
float delay = 0.2;
if (numTaps < 2)
{
[self performSelector:@selector(handleSingleTap) withObject:nil afterDelay:delay ];
[self.nextResponder touchesEnded:touches withEvent:event];
}
else if(numTaps == 2)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(handleDoubleTap) withObject:nil afterDelay:delay ];
}
}
答案 1 :(得分:2)
这将有助于定义单击和双击的方法
(void) handleSingleTap {}
(void) handleDoubleTap {}
那么在touchesEnded
中你可以根据点击次数调用适当的方法,但只能在延迟后调用handleSingleTap以确保没有执行双击:
-(void) touchesEnded(NSSet *)touches withEvent:(UIEvent *)event {
if ([touch tapCount] == 1) {
[self performSelector:@selector(handleSingleTap) withObject:nil
afterDelay:0.3]; //delay of 0.3 seconds
} else if([touch tapCount] == 2) {
[self handleDoubleTap];
}
}
在touchesBegan
中,取消对handleSingleTap的所有请求,以便第二次点击取消第一次点击对handleSingleTap
的通话,只会调用handleDoubleTap
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(handleSingleTap) object:nil];
答案 2 :(得分:0)
也许你可以使用一些时间间隔。等待事件调度(x)ms。如果您在该时间段内获得两次点击,请拨打双击。如果您只获得一次调度。