簡體   English   中英

如何在iPhone上檢測到兩根手指?

[英]How do I detect two fingers tap on the iPhone?

如何在iPhone上檢測到兩根手指?

如果您可以定位OS 3.2或更高版本,則可以使用UITapGestureRecognizer 它非常易於使用:只需配置它並將其附加到視圖中即可。 當執行手勢時,它將觸發gestureRecognizer目標的動作。

例:

UITapGestureRecognizer * r = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewWasDoubleTapped:)];
[r setNumberOfTapsRequired:2];
[[self view] addGestureRecognizer:r];
[r release];

然后你只需實現一個- (void) viewWasDoubleTapped:(id)sender方法,當[self view]被雙擊時,它將被調用。

編輯

我剛剛意識到你可能正在談論用兩根手指檢測一個水龍頭。 如果是這樣的話,你可以做到

[r setNumberOfTouchesRequired:2]

這種方法的主要優點是您不必創建自定義視圖子類

如果您的目標不是3.2+:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] == 2) {
        //etc
    }
}

multiTouchEnabled屬性設置為YES

如果您的要求允許,請使用UITapGestureRecognizer。 否則,在自定義UIView中實現以下UIResponder方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

跟蹤整個過程以查看有多少次觸摸以及它們是否超過了您的敲擊/拖動閾值。 您必須實現所有四種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM