Double-tap or two single-taps?

前端 未结 6 1792
轮回少年
轮回少年 2021-02-02 17:46

What is the time limit for two taps to be considered a double-tap, on the iPhone OS?

// Edit: Why is this important?

In order to handle single-tap and double-tap

相关标签:
6条回答
  • 2021-02-02 17:55

    I don't believe there is a default time-limit - it's whatever "feels" right when you try it out. Apple has an example here, where they don't specify a specific time either.

    0 讨论(0)
  • 2021-02-02 17:59

    On Developer Forums on developer.apple.com, an Apple developer said:

    • There's no constant time interval, but the system-wide definition is implicit today in the tapCount property of UITouch.
    • There is not default value for this delay and for touch-and-hold delay.
    • SpringBoard has the value hardcoded.

    I have submitted this to Apple as bug 68405.

    0 讨论(0)
  • 2021-02-02 18:04

    You can detect any number of tap by the tap gesture. No need of playing with NSTouches either. For all the user seeking for the solution here it is.

    These simple lines of code does the duty of single and double tap functionality.

      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];
    
    0 讨论(0)
  • 2021-02-02 18:09
    - (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
    

    for me this code is working prefectly fine.... :) the last post was around august 15 09.... but i was going through the same problem so thought to share the solution i found....

    0 讨论(0)
  • 2021-02-02 18:10

    I am not sure if this is the correct way of doing things but this code is working well for me.

    in my .h file:

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

    in my .m file:

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

    And this is where I add my tap listener (it is on a UIImageView )

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

    Good Luck and Happy Coding!

    0 讨论(0)
  • 2021-02-02 18:16

    In the Apple provided example of the iPhone Application Programming Guide: Event Handling (mentioned by user Italy) a delay of 0.3 is used:

    [self performSelector:@selector(handleSingleTap:) withObject:touchLoc afterDelay:0.3];
    
    0 讨论(0)
提交回复
热议问题