如果您的 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;}}
以上所述是小編給大家介紹的iOS touch事件區分單擊雙擊響應的方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
新聞熱點
疑難解答