簡體   English   中英

iOS-TapGestureRecognizer-Tap適用於整個屏幕而不適用於視圖

[英]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。
另外,讓它imageObjUIImageView對象應該具有userInteractionEnabled = YES

代替:

[[self view] addGestureRecognizer:tapGestureRecognizerImage];

你應該做:

[imageObj setUserInteractionEnabled:YES];
[imageObj addGestureRecognizer:tapGestureRecognizerImage];

通常,在要使用手勢對象的對象上使用-addGestureRecognizer:
假設您有一個名為myTapGestureUITapGestureRecognizer對象。

然后,使其工作...

  1. UILabel *lblSomeObj ,它將是:
    • [lblSomeObj addGestureRecognizer:myTapGesture];
  2. UIView *vwSomeObj ,它將是:
    • [vwSomeObj addGestureRecognizer:myTapGesture];
  3. 等等...

您需要包括以下代碼:

 [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.

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