iOS 響應(yīng)者鏈(Responder Chain)

終于搞清楚iOS響應(yīng)者鏈了

首先要清楚幾個(gè)關(guān)鍵字:UIResponderFirst responder

UIResponder是所有響應(yīng)對象的基類,在UIResponder類中定義了處理各種事件的接口。我們熟悉的UIApplication、 UIViewController、UIWindow和所有繼承自UIView的UIKit類都繼承自UIResponder,所以它們的實(shí)例都是可以構(gòu)成響應(yīng)者鏈的響應(yīng)者對象。

First responder(第一響應(yīng)者)指的是當(dāng)前接受觸摸的響應(yīng)者對象(通常是一個(gè)UIView對象),即表示當(dāng)前該對象正在與用戶交互,它是響應(yīng)者鏈的開端。整個(gè)響應(yīng)者鏈和事件分發(fā)的使命都是找出第一響應(yīng)者。

大概過程:

1.Events 發(fā)生
2.hitTest:withEvent:pointInside:withEvent: 查找第一響應(yīng)者
3.向上傳遞事件

發(fā)生事件 -- 查找事件源 -- 處理事件 大概這么個(gè)邏輯

iOS系統(tǒng)檢測到手指觸摸(Touch)操作時(shí)會(huì)將其打包成一個(gè)UIEvent對象,并放入當(dāng)前活動(dòng)Application的事件隊(duì)列,單例的UIApplication會(huì)從事件隊(duì)列中取出觸摸事件并傳遞給UIWindow來處理,

UIWindow首先會(huì)使用hitTest:withEvent:方法尋找此次Touch事件初始點(diǎn)所在的View。尋找的方法就是,從window開始,遍歷所有的子控件的hitTest:withEvent:方法,直到找到或者全部遍歷完成為止。
具體可以查看這個(gè)函數(shù)的說明

找到First responder后,運(yùn)行循環(huán)runLoop(這里可以忽略,就知道有個(gè)對象干了一件事情就行了)以消息的形式將事件發(fā)送給第一響應(yīng)者(調(diào)用touch方法),使其有機(jī)會(huì)首先處理事件。如果第一響應(yīng)者沒有進(jìn)行處理(沒實(shí)現(xiàn)touch方法),系統(tǒng)就將事件傳遞給響應(yīng)者鏈中的下一個(gè)響應(yīng)者(父節(jié)點(diǎn),父控件),看看它是否可以進(jìn)行處理。直到UIApplication,都沒人處理,就會(huì)被丟棄。官方的說法:事件分發(fā)(Event Delivery)

具體的介紹

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

不難看懂就不翻譯了,主要是自己回顧使用,如果有其他人看了,實(shí)在看不了英文的,找個(gè)翻譯軟件也行了

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的,還是有一點(diǎn)點(diǎn)的區(qū)別的。但是查找等方法還是一樣的,都是通過hitTest:withEvent:,只是找到第一響應(yīng)者后的事件分發(fā)(Event Delivery)變了。之前是直接調(diào)用第一響應(yīng)者的Touch事件的,但是現(xiàn)在找的是注冊的target對象,調(diào)用注冊的selector方法。

對于添加手勢的情況也是一樣的,只是最后事件分發(fā)變了

14870477182709.jpg

參考鏈接:

  1. iOS 響應(yīng)者鏈 整理
  2. Responder一點(diǎn)也不神秘————iOS用戶響應(yīng)者鏈完全剖析
    3.響應(yīng)者鏈機(jī)制介紹
    英文版地址:Understanding cocoa and cocoa touch responder chain
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一. Hit-Testing 什么是Hit-Testing?對于觸摸事件, window首先會(huì)嘗試將事件交給事件觸...
    面糊閱讀 1,035評論 0 50
  • 用戶以多種方式操縱他們的iOS設(shè)備,例如觸摸屏幕或搖動(dòng)設(shè)備。 iOS會(huì)解釋用戶何時(shí)以及如何操作硬件并將此信息傳遞到...
    坤坤同學(xué)閱讀 4,120評論 7 19
  • 好奇觸摸事件是如何從屏幕轉(zhuǎn)移到APP內(nèi)的?困惑于Cell怎么突然不能點(diǎn)擊了?糾結(jié)于如何實(shí)現(xiàn)這個(gè)奇葩響應(yīng)需求?亦或是...
    Lotheve閱讀 59,483評論 51 604
  • 在iOS開發(fā)中經(jīng)常會(huì)涉及到觸摸事件。本想自己總結(jié)一下,但是遇到了這篇文章,感覺總結(jié)的已經(jīng)很到位,特此轉(zhuǎn)載。作者:L...
    WQ_UESTC閱讀 6,246評論 4 26
  • 4事件分發(fā)機(jī)制 iOS中的事件大概分為三種,分別是Milti-Touch Events, Motion Event...
    Kevin_Junbaozi閱讀 904評論 0 2

友情鏈接更多精彩內(nèi)容