![](/img/trans.png)
[英]Not able to register a tap on the screen using tapGestureRecognizer
[英]iOS - TapGestureRecognizer - Tap is applicable for the whole screen not for a view
在我的應用程序中,我有一個圖像和一個UITextView
。
我已經為兩個視圖創建了一個UITapGestureRecognizer
,但是問題是,無論我在屏幕上單擊什么位置,都只會執行與UITextView
關聯的方法。
即使單擊圖像,也只會執行與UITextView
關聯的UITapGestureRecognizer
方法。
以下是我實現的代碼:
UITapGestureRecognizer *tapGestureRecognizerImage = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTapFromImage:)];
[infobutton addGestureRecognizer:tapGestureRecognizerImage];
[[self view] addGestureRecognizer:tapGestureRecognizerImage];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTapFrom:)];
[messageOne addGestureRecognizer:tapGestureRecognizer];
[[self view] addGestureRecognizer:tapGestureRecognizer];
//The following are the methods associated
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer {
//Code to handle the gesture
NSLog(@"I am in handleTapFrom method");
}
- (void) handleTapFromImage: (UITapGestureRecognizer *)recognizer {
//Code to handle the gesture
NSLog(@"I am in handleTapFrom Image method");
[self.view makeToast:@"Your verification code does not match. Re-enter your verification code"];
}
我確定我在這里錯過了一些東西。
據我所知,情節提要中的關聯是正確的。
請糾正我我要去哪里了
謝謝你的時間
您不應該在self.view上添加手勢。
它應該添加到您要為其識別點擊事件的視圖上。
您正在[self view]
對象上設置了兩個Tap Gesture Objects。
另外,讓它imageObj
的UIImageView
對象應該具有userInteractionEnabled = YES
。
代替:
[[self view] addGestureRecognizer:tapGestureRecognizerImage];
你應該做:
[imageObj setUserInteractionEnabled:YES];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
通常,在要使用手勢對象的對象上使用-addGestureRecognizer:
假設您有一個名為myTapGesture
的UITapGestureRecognizer
對象。
然后,使其工作...
UILabel *lblSomeObj
,它將是:
[lblSomeObj addGestureRecognizer:myTapGesture];
UIView *vwSomeObj
,它將是:
[vwSomeObj addGestureRecognizer:myTapGesture];
您需要包括以下代碼:
[tapGestureRecognizerImage requireGestureRecognizerToFail:tapGestureRecognizer];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
只需將手勢添加到各個視圖中,而不要添加到self.view中。
UITapGestureRecognizer *tapGestureRecognizerImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFromImage:)];
[infobutton addGestureRecognizer:tapGestureRecognizerImage];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[messageOne addGestureRecognizer:tapGestureRecognizer];
在UIImageView對象上添加手勢,並確保將圖像視圖userInteractionEnabled
設置為YES
imageObj.userInteractionEnabled = YES;
[imageObj addGestureRecognizer:tapGestureRecognizerImage];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.