Double-tap or two single-taps?

前端 未结 6 1794
轮回少年
轮回少年 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 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!

提交回复
热议问题