iOS touch事件單擊雙擊區分響應

若是您的 iPhone 應用裏有個 view,既有單擊操做又有雙擊操做。用戶雙擊 view 時,老是先執行一遍單擊的操做再執行雙擊的操做。因此直接判斷時就會發現不能直接進入雙擊操做。下面是區分 touch 事件是單擊仍是雙擊的方法orm

-(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;
   }
}事件

相關文章
相關標籤/搜索