iOS touch事件单击双击区分响应

时间:2022-08-01 19:26:39

如果您的 iPhone 应用里有个 view,既有单击操作又有双击操作。用户双击 view 时,总是先执行一遍单击的操作再执行双击的操作。所以直接判断时就会发现不能直接进入双击操作。下面是区分 touch 事件是单击还是双击的方法

-(void)singleTap{
    NSLog(@"Tap 1 time");
}
-(void)doubleTap{
    NSLog(@"Tap 2 time");
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    
    NSTimeInterval delaytime = 0.4;//自己根据需要调整
    
   switch (touch.tapCount) {
       case 1:
           [self performSelector:@selector(singleTap) withObject:nil afterDelay:delaytime];
           break;
       case 2:{
           [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
           [self performSelector:@selector(doubleTap) withObject:nil afterDelay:delaytime];
           
       }
            break;
       default:
           break;
   }
}