iOS 响应者链(Responder Chain)

终于搞清楚iOS响应者链了

首先要清楚几个关键字:UIResponderFirst responder

UIResponder是所有响应对象的基类,在UIResponder类中定义了处理各种事件的接口。我们熟悉的UIApplication、 UIViewController、UIWindow和所有继承自UIView的UIKit类都继承自UIResponder,所以它们的实例都是可以构成响应者链的响应者对象。

First responder(第一响应者)指的是当前接受触摸的响应者对象(通常是一个UIView对象),即表示当前该对象正在与用户交互,它是响应者链的开端。整个响应者链和事件分发的使命都是找出第一响应者。

大概过程:

1.Events 发生
2.hitTest:withEvent:pointInside:withEvent: 查找第一响应者
3.向上传递事件

发生事件 -- 查找事件源 -- 处理事件 大概这么个逻辑

iOS系统检测到手指触摸(Touch)操作时会将其打包成一个UIEvent对象,并放入当前活动Application的事件队列,单例的UIApplication会从事件队列中取出触摸事件并传递给UIWindow来处理,

UIWindow首先会使用hitTest:withEvent:方法寻找此次Touch事件初始点所在的View。寻找的方法就是,从window开始,遍历所有的子控件的hitTest:withEvent:方法,直到找到或者全部遍历完成为止。
具体可以查看这个函数的说明

找到First responder后,运行循环runLoop(这里可以忽略,就知道有个对象干了一件事情就行了)以消息的形式将事件发送给第一响应者(调用touch方法),使其有机会首先处理事件。如果第一响应者没有进行处理(没实现touch方法),系统就将事件传递给响应者链中的下一个响应者(父节点,父控件),看看它是否可以进行处理。直到UIApplication,都没人处理,就会被丢弃。官方的说法:事件分发(Event Delivery)

具体的介绍

- ([UIView]hitTest:([CGPoint]point withEvent:([UIEvent] *)event;

不难看懂就不翻译了,主要是自己回顾使用,如果有其他人看了,实在看不了英文的,找个翻译软件也行了

Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.

This method traverses the view hierarchy by calling the [pointInside:withEvent:] method of each subview to determine which subview should receive a touch event. If [pointInside:withEvent:]returns YES, then the subview’s hierarchy is similarly traversed until the frontmost view containing the specified point is found. If a view does not contain the point, its branch of the view hierarchy is ignored. You rarely need to call this method yourself, but you might override it to hide touch events from subviews.

This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.

Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. This can occur if the current view’s [clipsToBounds] property is set to NOand the affected subview extends beyond the view’s bounds.

UIControl,UITapGesturer

对于UIButton那类,继承自UIControl的,还是有一点点的区别的。但是查找等方法还是一样的,都是通过hitTest:withEvent:,只是找到第一响应者后的事件分发(Event Delivery)变了。之前是直接调用第一响应者的Touch事件的,但是现在找的是注册的target对象,调用注册的selector方法。

对于添加手势的情况也是一样的,只是最后事件分发变了

14870477182709.jpg

参考链接:

  1. iOS 响应者链 整理
  2. Responder一点也不神秘————iOS用户响应者链完全剖析
    3.响应者链机制介绍
    英文版地址:Understanding cocoa and cocoa touch responder chain
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一. Hit-Testing 什么是Hit-Testing?对于触摸事件, window首先会尝试将事件交给事件触...
    面糊阅读 840评论 0 50
  • 用户以多种方式操纵他们的iOS设备,例如触摸屏幕或摇动设备。 iOS会解释用户何时以及如何操作硬件并将此信息传递到...
    坤坤同学阅读 4,027评论 7 19
  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 57,808评论 51 602
  • 在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:L...
    WQ_UESTC阅读 6,090评论 4 26
  • 4事件分发机制 iOS中的事件大概分为三种,分别是Milti-Touch Events, Motion Event...
    Kevin_Junbaozi阅读 819评论 0 2