iOS shouldRecognizeSimultaneouslyWithGestureRecognizer 调用机制探索

shouldRecognizeSimultaneouslyWithGestureRecognizer 经常会看到,但是一直没有弄清楚其中的原理和运行机制,今天专门研究下

其运行规律

我们准备三个视图,如下,红色的是绿色视图的父视图,绿色视图

是蓝色视图的父视图,为了探索 shouldRecognizeSimultaneouslyWithGestureRecognizer

并且,每一个视图中都有如下代码

复制代码
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigClick)];
        tap.numberOfTapsRequired = 1;
        self.backgroundColor = [UIColor greenColor];
        [self addGestureRecognizer:tap];
        tap.delegate = self;
    }
    return self;
}

- (void)bigClick
{
    NSLog(@"点击中间的视图中间的视图");
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

为了测试不同的情况,我们将 shouldRecognizeSimultaneouslyWithGestureRecognize方法返回不同的值,并在三个视图中设置不同的组合,来测试方法相应的效果

下面我们直接列出结果,并将结果列出一个表格

其中的YES或者NO都是该视图 shouldRecognizeSimultaneouslyWithGestureRecognize 代理方法

的返回值

点击小视图(蓝视图)的时候

视图大 视图中 视图小 有响应的视图
NO NO YES 中、小
NO YES YES 大、 中、小
YES NO YES 大、中、小
YES YES YES 大、 中、小
NO NO NO
NO YES NO 中、小
YES YES NO 大、 中、小
YES NO NO 大、 小

点击中视图(绿色)的时候

视图大 视图中 有响应的视图
NO NO
YES NO 大、中
NO YES 大、 中
YES YES 大、 中

以上,我们可以得出结论:

如果某个视图的 shouldRecognizeSimultaneouslyWithGestureRecognize

返回了YES(前提是该手势设置了代理,scrollView自带手势已经设置过代理是scrollView 自身),并且该视图是第一响应这,则手势继续向下层视图传递。

如果某个视图 shouldRecognizeSimultaneouslyWithGestureRecognize 返回了YES,但是该视图不是第一响应者,则传递到该视图并响应该视图的手势之后,就停止继续向下传递。注意:返回NO虽然不向下传递了,如果该视图的父视图

也返回了YES,则该视图的俯视图也会响应。

相关推荐
2501_9160088935 分钟前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214339 分钟前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
2501_916008894 小时前
uni-app iOS 日志与崩溃分析全流程 多工具协作的实战指南
android·ios·小程序·https·uni-app·iphone·webview
明月(Alioo)5 小时前
机器学习入门,用Lima在macOS免费搭建Docker环境,彻底解决镜像与收费难题!
macos·docker·容器
小江村儿的文杰5 小时前
UE4 Mac构建编译报错 no template named “is_void_v” in namespace “std”
macos·ue4
2501_915921436 小时前
iOS混淆工具实战 在线教育直播类 App 的课程与互动安全防护
android·安全·ios·小程序·uni-app·iphone·webview
MonkeyKing_sunyuhua6 小时前
mac怎么安装uv工具
python·macos·uv
Digitally7 小时前
没 iCloud, 如何数据从iPhone转移到iPhone
ios·iphone·icloud
猫头虎7 小时前
IDE mac M芯片安装报错:如何解决“InsCode.app 已损坏”,无法打开
ide·vscode·macos·inscode·编辑器·idea·mac
笑尘pyrotechnic8 小时前
push pop 和 present dismiss
macos·ui·ios·objective-c·cocoa