hitTest和pointInside如何响应用户点击事件
hitTest和pointInside如何响应用户点击事件
处理机制
iOS事件处理,首先应该是找到能处理点击事件的视图,然后在找到的这个视图里处理这个点击事件。
处理原理如下:
• 当用户点击屏幕时,会产生一个触摸事件,系统会将该事件加入到一个由UIApplication管理的事件队列中
• UIApplication会从事件队列中取出最前面的事件进行分发以便处理,通常,先发送事件给应用程序的主窗口(UIWindow)
• 主窗口会调用hitTest:withEvent:方法在视图(UIView)层次结构中找到一个最合适的UIView来处理触摸事件
(hitTest:withEvent:其实是UIView的一个方法,UIWindow继承自UIView,因此主窗口UIWindow也是属于视图的一种)
• hitTest:withEvent:方法大致处理流程是这样的:
首先调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内:
▶ 若pointInside:withEvent:方法返回NO,说明触摸点不在当前视图内,则当前视图的hitTest:withEvent:返回nil
▶ 若pointInside:withEvent:方法返回YES,说明触摸点在当前视图内,则遍历当前视图的所有子视图(subviews),调用子视图的hitTest:withEvent:方法重复前面的步骤,子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历,直到有子视图的hitTest:withEvent:方法返回非空对象或者全部子视图遍历完毕:
▷ 若第一次有子视图的hitTest:withEvent:方法返回非空对象,则当前视图的hitTest:withEvent:方法就返回此对象,处理结束
▷ 若所有子视图的hitTest:withEvent:方法都返回nil,则当前视图的hitTest:withEvent:方法返回当前视图自身(self)
• 最终,这个触摸事件交给主窗口的hitTest:withEvent:方法返回的视图对象去处理。我的微信号iOS开发:iOSDevTip
案列分析
在UIViewController的self.view上加载一个LGFirstView
LGFirstView上面有一个UIButton我们叫它buttonFirst
然后,self.view上加载一个LGSecondView,刚好盖在LGFirstView上面
LGSecondView上面也有一个UIButton我们叫它buttonSecond
正常情况下:
用户点击LGSecondView(点击的点不在buttonSecond上,但是在buttonFirst撒很难过吗),事件处理流程如下:
1)调用UIWindow的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在UIWindow上面。
2)去遍历UIWindow上面的子视图,也就是self.view。同样也是调用self.view的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在self.view上面。
3)去遍历self.view上的子视图,也就是LGFirstView和LGSecondView。(注意:子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历)。
4)所以先调用LGSecondView的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在LGSecondView上面。(此时不会再去遍历LGFirstView,所以正如你所愿buttonFirst的点击事件也不会被调用)
5)还没有结束,接着回去遍历LGSecondView上的所有子视图,结果所有子视图的hitTest:withEvent:方法都返回nil,因为LGSecondView上面只有secondButton,而点击的点不在secondButton。
6)最终hitTest:withEvent:方法返回当前视图自身(self),而LGSecondView没有事件要处理。整个过程结束。
如果我们想让buttonFirst也响应点击事件怎么办?
方法一:
我们在LGSecondView加入如下代码:
#pragma mark - 方法一
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self)
{
return nil;
}
else
{
return hitView;
}
}
我们再来分析一下:
还是这个场景,用户点击LGSecondView(点击的点不在buttonSecond上,但是在buttonFirst撒很难过吗),事件处理流程如下:
1)调用UIWindow的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在UIWindow上面。
2)去遍历UIWindow上面的子视图,也就是self.view。同样也是调用self.view的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在self.view上面。
3)去遍历self.view上的子视图,也就是LGFirstView和LGSecondView。(注意:子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历)。
4)所以先调用LGSecondView的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在LGSecondView上面。
5)但是,注意了,这里有个但是, UIView *hitView = [super hitTest:point withEvent:event];就是这句代码发挥了作用。如果hitView是LGSecondView的话,就不处理点击事件。(这跟userInteractionEnabled=NO是不一样的,userInteractionEnabled=NO,LGSecondView上的buttonSecond也不会响应点击事件了。)
6)这个时候会去调用LGFirstView的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在LGFirstView上面。
7)再去遍历LGFirstView上面的子视图,也就是buttonFirst,调用buttonFirst的hitTest:withEvent:方法,hitTest:withEvent:方法会调用pointInside:withEvent:方法。此时pointInside:withEvent:返回YES,说明触摸事件在buttonFirst上面。
8)再去遍历buttonFirst上的所有子视图,结果所有子视图的hitTest:withEvent:方法都返回nil,说明点击就在buttonFirst,buttonFirst就用响应的点击方法。
方法二
在LGSecondView.m
@interface LGSecondView ()
@property (nonatomic, strong) NSMutableArray *subControlsArray;
@end
@implementation LGSecondView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.subControlsArray = [NSMutableArray array];
}
return self;
}
#pragma mark - 方法二
- (void)addSubview:(UIView *)view{
[super addSubview:view];
if ([view isKindOfClass:[UIControl class]]) {
[self.subControlsArray addObject:view];
}
}
//set self not response action and self subviews response action
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
{
BOOL toNext = YES;
for (UIView *view in _subControlsArray) {
if (CGRectContainsPoint(view.frame, point)) {
toNext = NO;
break;
}
}
return !toNext;
}
hitTest和pointInside如何响应用户点击事件的更多相关文章
- listview 模仿用户点击事件。
正确的方法 gvFlow.post(new Runnable() { @Override public void run() { gvFlow.performItemClick(gvFlow.getC ...
- DuiLib笔记之自定义标题栏以及响应按钮点击事件
在博文DuiLib笔记,基于WindowImplBase的基础模板的基础上,修改皮肤文件如下 <?xml version="1.0" encoding="utf-8 ...
- DOM--4 响应用户操作和事件(2)
自定义事件 //旧的方法 //创建 var event = document.createEvent('Event'); //初始化 event.initEvent('build', true, tr ...
- DOM--4 响应用户操作和事件(1)
简介 事件:事件并不是以"on"开头的.例如,onclick引用的是一个对象的属性,click才是事件. 事件侦听器:当指定的事件发生时会执行的函数或方法. 事件注册:为DOM元素 ...
- android map高德地图显示多个点,并且每个marker点可以响应鼠标点击事件,处理跳转页面
定义一个数组,然后将要显示的markers放进数组里面,让后循环每个marke对象,赋予监听事件,在监听事件里面写其它需要的功能: js举例: var arr = new Arry(); var ma ...
- CefSharp For WPF响应页面点击事件
初始化 <!--浏览器--> <cefSharpWPF:ChromiumWebBrowser Name="webBrowser" Grid.Row="0 ...
- Electron 设置 -webkit-app-region 后无法响应鼠标点击事件的解决方式
参考博客:https://blog.csdn.net/qq_20264891/article/details/87721163
- 在Activity中响应ListView内部按钮的点击事件
最近交流群里面有人问到一个问题:如何在Activity中响应ListView内部按钮的点击事件,不要在Adapter中响应? 对于这个问题,我最初给他的解答是,在Adapter中定义一个回调接口,在A ...
- 在Activity中响应ListView内部按钮的点击事件的两种方法!!!
在Activity中响应ListView内部按钮的点击事件的两种方法 转载:http://www.cnblogs.com/ivan-xu/p/4124967.html 最近交流群里面有人问到一个问题: ...
随机推荐
- mac pro下iterm快捷键(转)
标签 新建标签:command + t 关闭标签:command + w 切换标签:command + 数字 command + 左右方向键 切换全屏:command + enter 查找:comma ...
- Add an Item to the New Action 在新建按钮中增加一个条目
In this lesson, you will learn how to add an item to the New Action (NewObjectViewController.NewObje ...
- centOS极简安装并启动ngnix
我在网上看到过很多种方法在centOS中安装nginx,比如像这样的: 这种方式太麻烦了,看了很恶心. 我在官网看到有这样一种操作,可谓极简.特此记录一下:(官网教程地址:http://nginx.o ...
- cssW3c书写规范
书写规范:{ 1.选择器名和{大括号之间必须空格(p {}) 2.属性名与之后的:之间不允许空格,:与属性值之间必须空格( p{font-size: 20px;}) 3.并集选择器的每个选择器声明必须 ...
- SQL server利用脚本添加链接服务器,可设置别名
USE [master]GO EXEC master.dbo.sp_addlinkedserver @server = N'你的别名', @srvproduct=N'', @provider=N'SQ ...
- 文本处理工具awk
目录 gawk:模式扫描和处理语言 awk语言 awk工作原理 print awk变量 自定义变量 printf命令 awk操作符 awk的条件判断 awk的循环 awk的数组 awk的函数 awk调 ...
- Linux内核源码分析--内核启动之zImage自解压过程【转】
转自:https://www.cnblogs.com/pengdonglin137/p/3838245.html 阅读目录(Content) zImage来历 piggy.gz压缩文件的特点 vmli ...
- 学习笔记:Django开发网上教育平台(参考了慕课网的教学视频)
第一步:进行环境的搭建(用到的IDE:pycharm ,数据库为mysql.nacicat.编辑语言python3.7.以及自己配置的虚拟环境venvpy37) Django==2.2 配置好 ...
- vue跨域
比如 我要请求的地址是https://edu.51cto.com/center/seckill/index/get-seckill-data 首先去 config ==> index.js 添加 ...
- poj 1064 Cable master 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...