2009-08-15 83 views
13

在iPhone OS上,两次敲击被认为是双击的时间限制是多少?双击或两个单击?

//编辑:为什么这很重要?

为了以不同的方式处理单击和双击,Apple的指南说在第一次点击时有一些“合理的”间隔做performSelector...afterDelay(如果第二次点击被检测到,则稍后取消)。

问题是,如果间隔太短(0.1),即使在双击时(如果只依赖于tapCount,也就是说),将执行单击操作。如果时间太长(0.8),那么当没有可能进行双击时,用户将不必要地等待单击被识别。

它必须是正好正确的号码,以最佳状态工作,但绝对不小,或者有一个为错误的机会(同时单输出和双抽头)。

+0

您可能想要指定您询问的平台。 – retracile 2009-08-15 18:30:36

+0

缩回,检查标签。 – JoshJordan 2009-08-15 18:31:28

+0

@retracile:在这个问题上有一个“iphone”标签(当然,也许35秒前,当你发布你的评论时,它就不存在);所以,我猜它是在iphone平台上;-) – 2009-08-15 18:31:55

回答

11

您可以通过点按手势检测任意数量的点按。无需与NSTouches一起玩。对于所有在这里寻求解决方案的用户来说都是如此。

这些简单的代码行代码具有单击和双击功能的功能。

UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(doubleTapped:)]; 
    doubleTapRecg.delegate = self; 
    doubleTapRecg.numberOfTapsRequired = 2; 
    doubleTapRecg.numberOfTouchesRequired = 1; 
    [view addGestureRecognizer:doubleTapRecg]; 


    UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc] 
             initWithTarget:self 
             action:@selector(tapped:)]; 
    tapRecg.delegate = self; 
    tapRecg.numberOfTapsRequired = 1; 
    tapRecg.numberOfTouchesRequired = 1; 
    [view addGestureRecognizer:tapRecg]; 
    [tapRecg requireGestureRecognizerToFail:doubleTapRecg]; 


    [doubleTapRecg release]; 
    [tapRecg release]; 
+2

我正在改变对此的接受答案,因为它似乎是现在这样做的正确方法。但在评论以前的答案并称其他答案为垃圾之前,请记住,它们在您的UITapGestureRecognizer甚至存在之前的3年之前发布;) – 2013-03-09 20:55:09

+0

这是我的另一个帐户:D – yunas 2013-07-25 09:29:55

+0

没有手动计时器或延迟,迫使您决定多少msecs等待,真棒! – cprcrack 2013-10-25 13:11:17

1

我不相信有一个默认的时间限制 - 它是任何“感觉”,当你尝试了。苹果有一个例子here,他们也没有指定具体的时间。

+0

必须有一个限制,因为如果点击是相隔一秒的话,UITouch的tapCount不会增加,但是如果它们靠得很近,它的确会有所增加。 – 2009-08-15 18:39:41

5

论developer.apple.com开发者论坛,苹果的开发者表示:

  • 有没有固定的时间间隔,但全系统的定义是隐含在今天UITouch的tapCount财产。
  • 此延迟和触摸并保持延迟没有默认值。
  • SpringBoard的硬编码值。

我已将此提交给Apple,作为bug 68405

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

    UITouch *touch = [touches anyObject]; 

    NSUInteger tapCount = [touch tapCount]; 

    switch (tapCount) { 
     case 1: 
      [self performSelector:@selector(singleTapMethod) withObject:nil afterDelay:.4]; 
      break; 
     case 2: 
      [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapMethod) object:nil]; 
      [self performSelector:@selector(doubleTapMethod) withObject:nil afterDelay:.4]; 
      break; 
     case 3: 
      [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doubleTapMethod) object:nil]; 
      [self performSelector:@selector(tripleTapMethod) withObject:nil afterDelay:.4]; 
      break; 
     case 4: 
      [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tripleTapMethod) object:nil]; 
      [self quadrupleTap]; 
      break; 
     default: 
      break; 
    } 

} 
@end 

对于我这个代码工作prefectly罚款.... :)过去后约为09年8月15日.... 但我正在经历这样认为分享我找到了解决同样的问题....

0

我不知道这是否是做事的正确方法,但这段代码对我来说效果很好。

@interface MyViewController : UIViewController { 

int tapCount; 

} 
@property (nonatomic, assign)int tapCount; 
@end 
在我的.m文件

在我.h文件中

@synthesize tapCount; 

- (void)tapGesture:(UIGestureRecognizer*)gesture { 
    tapCount = tapCount + 1; 
    [self performSelector:@selector(correctTapCount) withObject:nil afterDelay:0.3]; 


} 
-(void)correctTapCount { 
    if (tapCount == 1) { 
     NSLog(@"Single Tap"); 
    } 
    if (tapCount == 2) { 
     NSLog(@"Double Tap"); 
    } 

    //reset TapCount 
    tapCount = 0; 
} 

而这正是我加入我的自来水监听器(它是一个UIImageView)

//add tap listener 
carImage.userInteractionEnabled = YES; 
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; 
[carImage addGestureRecognizer:tap]; 
[tap release]; 

好运和快乐编码!