代码之家  ›  专栏  ›  技术社区  ›  Jaka Jančar

双击还是两个单点击?

  •  13
  • Jaka Jančar  · 技术社区  · 16 年前

    在iPhone操作系统上,两次点击被视为双击的时间限制是什么?

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

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

    问题是,如果间隔太短(0.1),即使在双击时也会执行单点击操作(如果仅依赖TapCount,即)。如果太长(0.8),用户将不必要地等待识别单点,而此时不可能进行双点。

    它必须是 确切地 正确的数字,以优化工作,但绝对不能小,否则有可能出现错误(同时单点击和双点击)。

    6 回复  |  直到 13 年前
        1
  •  11
  •   Andrew Barber Eric Lafortune    13 年前

    您可以通过点击手势检测任意数量的点击。不需要玩 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
  •  10
  •   markpasc    15 年前
    - (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
    

    对我来说,这段代码工作得很好……:)最后一个帖子是在2009年8月15日左右。 但我也遇到了同样的问题,所以我想和大家分享我发现的解决方案……

        3
  •  8
  •   Travis    16 年前

    在苹果提供的例子中 iPhone Application Programming Guide: Event Handling (用户意大利提到)延迟 0.3 使用:

    [self performSelector:@selector(handleSingleTap:) withObject:touchLoc afterDelay:0.3];
    
        4
  •  5
  •   Jaka Jančar    16 年前

    在developer.apple.com的开发者论坛上,一位苹果开发者说:

    • 虽然没有固定的时间间隔,但是系统范围的定义在今天的uitouch的tapcount属性中是隐式的。
    • 此延迟和触摸保持延迟没有默认值。
    • 跳板具有硬编码的价值。

    我已将此作为 bug 68405 .

        5
  •  1
  •   Itay    16 年前

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

        6
  •  0
  •   Louie    15 年前

    我不确定这是不是正确的做事方式,但这段代码对我来说工作得很好。

    在我的.h文件中:

     @interface MyViewController : UIViewController { 
    
     int tapCount; 
    
     } 
     @property (nonatomic, assign)int tapCount; 
     @end
    

    在.m文件中:

    @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;
    }
    

    这是我添加tap侦听器的地方(它在uiImageView上)

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

    祝您好运,编码愉快!