2010-04-14 29 views
4

我試圖在視圖上檢測到雙擊,但是當雙擊時,第一個水龍頭會在TouchesBegan上觸發一個動作,因此,在檢測到雙擊之前總會檢測到單擊第一。iphone - 雙擊失效安全方式

我該如何以一種只檢測雙擊的方式做到這一點?

我不能使用OS 3.x手勢,因爲我必須使它與舊版本的操作系統兼容。

謝謝

回答

14

從scrollViewSuite示例代碼的tapZoom例如一些摘錄:

首先,函數揭開序幕的東西,一旦觸摸結束:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 1) { 

      [self performSelector: @selector(handleSingleTap) 
         withObject: nil 
         afterDelay: 0.35]; // after 0.35s we call it a single tap 

    } else if([touch tapCount] == 2) { 

      [self handleDoubleTap]; 
    } 

} 

二:攔截消息,如果超時期間發生新的觸摸:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    [NSObject cancelPreviousPerformRequestsWithTarget: self 
              selector: @selector(handleSingleTap) 
               object: nil]; 
} 

也參見:http://developer.apple.com/iphone/library/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomingByTouch/ZoomingByTouch.html#//apple_ref/doc/uid/TP40008179-CH4-SW1

這裏:(滾動視圖套房) http://developer.apple.com/iphone/library/samplecode/ScrollViewSuite/Introduction/Intro.html

+0

謝謝。抱歉耽擱了。 – SpaceDog 2010-07-13 01:41:48

2

您是否正在查看tapCount?例如:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
     UITouch *touch = [[event allTouches] anyObject]; 
     if (touch.tapCount == 2) { 
        //double-tap action here 
     } 
} 
6

編輯:我錯過了你說你不能使用3.x的手勢點,所以這是一個無效的回答你的問題,但我要離開如果有人使用3.x手勢可能會從中受益。

您可以創建兩個手勢識別,一個單一的水龍頭,一個用於雙擊:

UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)]; 
singleTapGesture.cancelsTouchesInView = NO; 
singleTapGesture.delaysTouchesEnded = NO; 
singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap 
singleTapGesture.numberOfTapsRequired = 1; 
[self.view addGestureRecognizer:singleTapGesture]; 
[singleTapGesture release]; 

UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesTwo:)]; 
doubleTapGesture.cancelsTouchesInView = NO; 
doubleTapGesture.delaysTouchesEnded = NO; 
doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap 
doubleTapGesture.numberOfTapsRequired = 2; 
[self.view addGestureRecognizer:doubleTapGesture]; 
[doubleTapGesture release]; 

,然後來了一拳:

[singleTapGesture requireGestureRecognizerToFail : doubleTapGesture]; 

最後一行,讓您單隻有雙擊失敗時,點按處理程序才能工作。所以,你的應用程序中只有單擊和雙擊。

+0

真的是一拳...同時使用兩種手勢[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];節省了很多時間。 – 2012-04-27 10:41:48