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

相关推荐
Lvan的前端笔记6 小时前
ios:版本号和Build构建号
ios
2601_9609067212 小时前
科技“无人区”跋涉,难度高、不确定性大
windows·macos·pycharm·myeclipse
鹤卿12312 小时前
「iOS」3GShare总结
macos·ios·objective-c·cocoa
Lvan的前端笔记13 小时前
ios:区分 App Store / Ad Hoc / Development
ios
AI导出鸭14 小时前
如何让deepseek生成word文档 ?「AI 导出鸭」苹果版:从API流式解析到Pages级渲染,硬核攻克公式裂变与表格回流的终极方案。
人工智能·chatgpt·word·cocoa·ai导出鸭
Lvan的前端笔记15 小时前
ios:证书配置
ios
FeliksLv18 小时前
从 +load 到 Swift Macros:自动注册的演进与实践
ios·objective-c·swift
嵌入式小周18 小时前
Genymotion 安卓模拟器在 Intel 芯片 Mac 上的运行(附带下载方式)
android·macos
Lvan的前端笔记19 小时前
ios:GitHub Actions云端Xcode26打包上架TestFlight
macos·github·xcode
寒水馨19 小时前
macOS下载、安装godot-4.7.1-stable(附安装包Godot_v4.7.1-stable_macos.universal.zip)
macos·3d·游戏引擎·godot·跨平台·游戏开发·2d