iOS 事件機(jī)制

事件

iOS 將事件分為三類:

  • Touch
  • Motion
  • Remote
    像耳機(jī)線控……

Touch 事件

Touch 事件的過(guò)程:事件產(chǎn)生 ==》 事件分發(fā) ==》 事件響應(yīng)

事件產(chǎn)生

iOS每產(chǎn)生一個(gè)事件都會(huì)生成一個(gè) UIEvent 對(duì)象,它記錄了事件的類型( UIEventType / UIEventSubtype (主要用在Motion和Remote) )、時(shí)間、幾個(gè)手指觸控等信息
當(dāng)手指觸摸屏幕時(shí),每個(gè)手指都會(huì)產(chǎn)生一個(gè) UITouch 對(duì)象,它保存著跟手指相關(guān)的信息,如觸摸的位置、時(shí)間、階段( UITouchPhase )等
UIEvent 和 UITouch 關(guān)系 -- UIEvent 有一方法 - allTouches ,返回 NSSet 集合的一組 UITouch 對(duì)象,即一個(gè) UIEvent 包含一個(gè)或多個(gè) UITouch 對(duì)象

確定點(diǎn)擊對(duì)象

Hit Test -- iOS 通過(guò) Hit Test 來(lái)尋找觸摸點(diǎn)下面的 view 是什么

[UIView class]
- hitTest:withEvent:

Hit Test小結(jié):

  • 從 UIWindow 開(kāi)始,先父 view 后子 view
  • subViews 按照逆順序遍歷
  • 在代碼中是嵌套調(diào)用


如上圖,當(dāng)我們點(diǎn)擊 view4 的區(qū)域,有

hit test from view TestWindow
hit test from view 0
hit test from view 2
return hit view (null), self view 2
hit test from view 1
hit test from view 4
return hit view 4, self view 4
return hit view 4, self view 1
return hit view 4, self view 0
return hit view 4, self view TestWindow

事件分發(fā)

UIApplication 和 UIWindow 有方法 - sendEvent: ,用于把事件分發(fā)到 hitTest View

UIApplication == sendEvent: ==> UIWindow == sendEvent: ==> hitTest View

事件響應(yīng)

能響應(yīng)事件的類必須繼承于一個(gè)類 -- UIResponder,UIApplication / UIViewController / UIView( UI ) / AppDelegate 都繼承于 UIResponder
通常來(lái)說(shuō),我們首先找到的 hitTest View,就是 Touch 事件的第一個(gè) Responder

UIResponder 的響應(yīng)過(guò)程

觸摸開(kāi)始 ==》 觸摸移動(dòng) ==》 觸摸結(jié)束 ,還有觸摸取消

- touchesBegan:withEvent:
- touchesMoved:withEvent:
- touchesEnded:withEvent:
- touchesCancelled:withEvent:
UIResponder 的響應(yīng)順序

響應(yīng)鏈 ( Responder Chain ) 即一系列關(guān)聯(lián)的響應(yīng)對(duì)象 ( a series of linked responder objects )
first responder ==> next responder ==> ... ==> UIWindow ==> UIApplication ==> AppDelegate ==> 丟棄
subView/hitTestView ==> superView/VC.view ==> VC ==> VC' superView ==> root VC ==> window ==> application ==> AppDelegate ==> 丟棄

  • 如果想事件繼續(xù)傳遞下去,可以調(diào)用 [super touchesBegan:touches withEvent:event],不建議直接調(diào) [self.nextResponder touchesBegan:touches withEvent:event]
UIView 不響應(yīng)事件的條件
  • userInteractionEnabled = NO
  • hidden = YES
  • alpha ( 0-0.01 )

UIView 加大點(diǎn)擊區(qū)域

假設(shè)一個(gè)按鈕,希望能在其顯示區(qū)域的一定范圍外,也能響應(yīng)點(diǎn)擊事件

  • 最直觀的做法:加大透明按鈕
  • 事件機(jī)制 hitTest
    在父 view 的 - hitTest:withEvent: 方法中,判斷如果點(diǎn)擊的point 在要求的 rect 中時(shí),直接返回 button ,否則返回 [super hitTest:point withEvent:event] 繼續(xù)傳遞
    • pointInside:withEvent:
  • hitTest:withEvent: 方法會(huì)遞歸地調(diào)用 - pointInside:withEvent: 方法,- pointInside:withEvent: 用來(lái)判斷觸摸的 point 是否在 view 的范圍內(nèi)
    button 可以重寫這一方法
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGFloat buttonExtraPadding = 20;

    CGPoint convertPoint = [self convertPoint:point toView:self.superview];
    CGRect targetRect = CGRectInset(self.frame, - buttonExtraPadding, - buttonExtraPadding);
    if (CGRectContainsPoint(targetRect, convertPoint))
    {
        return YES;
    }

    return [super pointInside:point withEvent:event];  
}

子 view 超過(guò)父 view 范圍

當(dāng)子 view 超出父 view 的范圍時(shí),在父 view 范圍內(nèi)的部分能夠響應(yīng)事件,超出父 view 部分則不能響應(yīng)事件
因?yàn)樵?hitTest View 時(shí),父 view 的 hitTest View 監(jiān)測(cè)到不在范圍內(nèi),因而也不會(huì)遞歸調(diào)用到子 view 的 hitTest

// 重寫父 view 的 - pointInside:withEvent: 方法,使當(dāng)確定 point 在子 view 范圍內(nèi)時(shí),返回 YES
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    // point 需要轉(zhuǎn)換到子 view 的坐標(biāo)系
    if ([_button pointInside:[self convertPoint:point toView:_button] withEvent:event])
    {
        return YES;
    }

    return [super pointInside:point withEvent:event];
}

全局監(jiān)控 touch 事件

  • UIWindow 子類
    UIWindow 有 - sendEvent: 方法,可以捕獲到所有的 Touch 事件
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 原文地址:http://zhoon.github.io/ios/2015/04/12/ios-event.html...
    大餅炒雞蛋閱讀 1,722評(píng)論 2 8
  • 好奇觸摸事件是如何從屏幕轉(zhuǎn)移到APP內(nèi)的?困惑于Cell怎么突然不能點(diǎn)擊了?糾結(jié)于如何實(shí)現(xiàn)這個(gè)奇葩響應(yīng)需求?亦或是...
    Lotheve閱讀 57,973評(píng)論 51 603
  • 在iOS開(kāi)發(fā)中經(jīng)常會(huì)涉及到觸摸事件。本想自己總結(jié)一下,但是遇到了這篇文章,感覺(jué)總結(jié)的已經(jīng)很到位,特此轉(zhuǎn)載。作者:L...
    WQ_UESTC閱讀 6,108評(píng)論 4 26
  • iOS的事件有好幾種:Touch Events(觸摸事件)、Motion Events(運(yùn)動(dòng)事件,比如重力感應(yīng)和搖...
    悟2023閱讀 668評(píng)論 0 1
  • 本文來(lái)自:http://ios.jobbole.com/84081/ 前言: 按照時(shí)間順序,事件的生命周期是這樣的...
    HackerOnce閱讀 2,858評(píng)論 1 10