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_9160074713 小时前
跨平台 App 安全,Flutter、RN、Unity、H5 混合应用加固
android·ios·小程序·https·uni-app·iphone·webview
新 一.13 小时前
uniapp在ios上真机测试运行
ios·uni-app
2501_9160074714 小时前
不越狱如何查看iOS 应用的详细信息及其文件目录结构
android·macos·ios·小程序·uni-app·cocoa·iphone
handsome091614 小时前
上传app store和IOS更新版本的实战流程
ios·打包上架
芒鸽16 小时前
macos上Rust 命令行工具鸿蒙化适配完全攻略
macos·rust·harmonyos
山有木兮啊16 小时前
VSCode Remote-SSH 连接Mac卡在初始化VSCode
vscode·macos·ssh
2501_9160088917 小时前
无需钥匙串快速创建 iOS 开发 / 发布证书 P12 CSR
android·ios·小程序·https·uni-app·iphone·webview
一个写bug的程序员18 小时前
Mac自启服务关闭方式
macos
梁下轻语的秋缘18 小时前
初学者避坑指南:Mac 虚拟机搭建 Keil5 STM32 环境 + 解决 ST-Link USB Command Error 报错
windows·stm32·macos
天荒地老笑话么18 小时前
macOS 终端:本机隐藏用户名,但 SSH 登录时仍显示(Oh My Zsh + agnoster,更安全)
windows·macos·网络安全