iOS - TapGestureRecognizer - 点击适用于整个屏幕而不是 View

标签 ios objective-c cocoa-touch uitapgesturerecognizer

在我的应用程序中,我有一张图片和一个 UITextView .
我创建了一个 UITapGestureRecognizer对于这两种 View ,但问题是无论我在屏幕上单击什么位置,只有与 UITextView 关联的方法被执行。
即使我点击图片,也只有 UITapGestureRecognizerUITextView 关联的方法被执行。

以下是我实现的代码:

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"];
}

我确定我在这里遗漏了一些东西。
据我所知, Storyboard中的关联是正确的。

请纠正我哪里出错了

谢谢你的时间

最佳答案

您不应该在 self.view 上添加手势。

它应该被添加到您想要识别点击事件的 View 中。

关于iOS - TapGestureRecognizer - 点击适用于整个屏幕而不是 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20538542/

相关文章:

iphone - 如何对日期数组进行排序?

ios - XCode TableViewController 在 Objective-C 中详细介绍

ios - CALayer 上的绘制文本在运行时不显示或崩溃

iphone - 这个变量的属性/返回值?

选择 UITabItem 时 iOS 应用程序崩溃

iphone - 在没有 tabBar 的情况下实现 tabbarcontroller 行为?

iOS 7 UITableView 问题 - 尝试设置滑动以删除已有单元格的单元格

ios - iCloud 和 CoreData : Notification when changed to iCloud Store (First Launch with existing iCloud Data)

iOS - 不能是连续或重复数字的 4 位整数的正则表达式?

ios - 删除图像中的 alpha channel