1、UIView中有两个方法是:
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; // default returns YES if point is in bounds
HItTest:withEvent:方法处理流程大致如下:
-
pointInside事件用来判断触摸点是否在当前视图的范围内,若不在则返回no,hitTest事件,则返回nil。
-
若第一次有子视图的hitTEst:withEvent:方法返回!nil,则当前视图的hitTest:withEvent:方法就返回此对象,处理结束。
-
若所有子视图的hitTest:withEvent:方法都返回nil,则当前视图的hitTest:withEvent:方法返回当前视图自身self。
寻找第一响应者原理如下:
- 1.当用户点击屏幕的时候,会产生触摸事件,系统会将该事件加入到一个由UIAplication管理的事件队列中
- 2.UIAplication 会从实践队列中取出最前面的事件进行分发以便处理,通常先发送给应用程序的主窗口UIWindow。
- 3.UIWindow会调用hitTest:withEvent:方法在视图UIView层次结构中找到一个最适合的UIView来处理触摸事件。hitTest:withEvent:其实是UIView的一个方法,UIWindow继承自UIView,因此主窗口UIWindow也是输入视图的一种。
逻辑图
响应事件逻辑图









网友评论