終于搞清楚iOS響應(yīng)者鏈了
首先要清楚幾個(gè)關(guān)鍵字:UIResponder,First 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ā)變了

參考鏈接: