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,则该视图的俯视图也会响应。

相关推荐
2301_8210465219 分钟前
Python与Go结合
ios·iphone
他们都不看好你,偏偏你最不争气1 小时前
【iOS】AFNetworking
开发语言·macos·ios·objective-c
zhanggui3 小时前
iOS Debug Symbols
ios·xcode·debug symbox
钟念4 小时前
【打包app】uniapp打包ios端和安卓端app
macos·objective-c·cocoa
库奇噜啦呼8 小时前
【iOS】static、const、extern关键字
ios
zzywxc7879 小时前
AI工具全景洞察:从智能编码到模型训练的全链路剖析
人工智能·spring·ios·prompt·ai编程
前端 贾公子10 小时前
《Vuejs设计与实现》第 16 章(解析器) 上
vue.js·flutter·ios
森之鸟17 小时前
Mac电脑上如何打印出字体图标
前端·javascript·macos
Digitally19 小时前
如何将大型音频文件从 iPhone 发送到不同的设备
ios·iphone
m_1368720 小时前
Mac M 系列芯片 YOLOv8 部署教程(CPU/Metal 后端一键安装)
yolo·macos