iOS使用hit-testing尋找觸摸的view。 Hit-Testing通過檢查觸摸點(diǎn)是否在關(guān)聯(lián)的view邊界內(nèi),如果在,則遞歸地檢查該view的所有子view。在層級(jí)上處于lowest(就是離用戶最近的view)且邊界范圍包含觸摸點(diǎn)的view成為hit-test view。確定hit-test view后,它傳遞觸摸事件給該view。
官方小例子事件響應(yīng)者鏈如下圖所示:

386B3F93-2F8B-4848-956C-0152E797B5F1.png
- 觸摸點(diǎn)在view A中,所以要先檢查子view B和C。
- 觸摸點(diǎn)不在view B中,但在C中,所以檢查C的子view D和E。
- 觸摸點(diǎn)不在D中,但在E中。View E是這個(gè)層級(jí)上處于lowest的- view的邊界范圍包含觸摸點(diǎn),所以它成為了hit-test view。
Hit-test view是處理觸摸事件的第一選擇,如果hit-test view不能處理事件,該事件將從事件響應(yīng)鏈中尋找響應(yīng)器,直到系統(tǒng)找到一個(gè)處理事件的對(duì)象。若不能處理,則就有事件傳遞鏈了,繼續(xù)看下面的事件傳遞鏈。
事件傳遞鏈如下圖所示:

iOS_responder_chain.png
左半圖:
initial view若不能處理事件,則傳到其父視圖view
view若不能處理,則傳到其父視圖,因?yàn)樗€不是最上層視圖
這里view的父視圖是view controller的view,因?yàn)檫@個(gè)view也不能處理事件,因此傳給view controller
若view controller也不能處理此事件,則傳到window
若window也不能處理此事件,則傳到app單例對(duì)象Application
若UIApplication單例對(duì)象也不能處理,則表示無效事件
右半圖:
initial view一直傳遞直到最上層view(原話:A view passes an event up its view controller’s view hierarchy until it reaches the topmost view.)
topmost view傳遞事件到它所在的控制器(原話:The topmost view passes the event to its view controller.)
view controller傳遞事件到topmost view的父視圖,重復(fù)前三步,走到到達(dá)root controller(原話:passes the event to its topmost view’s superview. Steps 1-3 repeat until the event reaches the root view controller.)
由root控制器傳遞事件到window(原話:The root view controller passes the event to the window object.)
若window也不能處理此事件,則傳到app單例對(duì)象Application
若UIApplication單例對(duì)象也不能處理,則表示無效事件
hit-testing還有兩點(diǎn)點(diǎn)個(gè)人感覺比較關(guān)鍵的點(diǎn)。
一、首先這個(gè)View它會(huì)檢查自己是否能夠接收觸摸事件(有三點(diǎn)不能接收觸摸事件:1、透明度<0.1,2、hidden=YES的時(shí)候,3、userInteractionEnabled = NO),然后它才會(huì)判斷當(dāng)前點(diǎn)在不在自己身上
二、當(dāng)它遍歷子控件的時(shí)候,是倒序遍歷的