国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

EventDelivery:TheResponderChain(事件傳遞,響應鏈)

2019-11-14 19:18:46
字體:
來源:轉載
供稿:網友

當我們設計app的時候,我們很可能想動態的響應事件。例如,觸摸一個擁有許多不同對象的屏幕,你要決定給哪個對象一個響應事件,怎么樣對象接收到事件。

當一個用戶產生事件發生時(如 點擊),UIKit產生一個對象,這個包含要被處理的對象的信息。接著把事件對象放到active app's(一個application對象,負責初始化用戶事件路由、從全局管理一個運行的application)事件隊列中。對于觸摸事件,這個對象是一組觸摸,打包放在UIEvent對象。對motion事件而言,事件對象根據你使用框架的不同和你感興趣的motion事件類型而有所差異。

一個事件對象沿一個明確的路徑傳遞,直到有個對象可以處理它。首先,單例UIApplication對象從隊列的頂部取一個事件,并進行分發處理。一般,UIApplication對象把事件發給應用的key window 對象,key window對象傳遞給一個initial對象來處理。這個initial對象取決于事件的類型。

  • Touch events。對于觸摸事件,window對象首先嘗試將事件發送給事件發生的view(hit-test view)。找到hit-test view的過程被稱為hit-testing(我們將在后邊談到)。
  • Motioin and remote control enents。這類事件,window對蝦ing發送shaking-motion或者remote control event給第一響應者處理。

事件路徑(event paths)的終極目標是找到一個對象來處理響應一個事件。因此,UIKit首先把這個事件發送給最適合處理這個時間的對象。對于觸摸事件,這個對象是hit-test view;對于其他事件,這個對象是第一響應者。下面的部分介紹關于確定hit-test view 和 第一響應者的更多細節。

Hit-Testing Returns the View Where a Touch Occurred(hit-test返回觸摸發生的view)。有的地方翻譯感覺很別扭,為了避免誤導(真的有人看嗎?),原文貼上。

iOS use hit-testing fo find the view that is under a touch. Hit-testing involves checking whether a touch is within the bounds fo any relevant(更多強調直接相關) view objects. If it is, it recursively checks all of that view's subviews. The lowest view in the view hierarchy that contains the touch point becomes the hit-test view. After iOS determines the hit-test view, it passes the touch event to that view for handling.

舉例說明,假設用戶在View E中觸摸,iOS通過用下面的次序檢查subviews來查找hit-test view:

  1. 觸摸在view A的bounds內,所以檢查subview B和C;
  2. 觸摸不在View B的bounds內,但在View C的bounds內,所以檢查subview D和E;
  3. 觸摸不在View D的bounds內,但在view E的bounds內。
    View E is the lowest view in the view hierarchy that contains the touch, so it becomes the hit-test view.

 hitTest:withEvent: 方法返回一個CGPoint和UIEvent的hit-test view。 hitTest:withEvent: 方法開始執行通過view本身調用 pointInside:withEvent: 方法。如果傳入pointInside:withEvent:的點在view的bounds內,pointInside:withEvent:返回YES。接著,在這個view的每個subview上調用hitTest:withEvent:

傳入hitTest:withEvent:的點不在view的bounds中得情況不細說了。

注意:一個觸摸事件在它的整個生命周期中和它的hit-test view相關聯,即使稍后觸摸移出了這個view。

hit-test view被給予了第一次來處理觸摸事件的機會。如果hit-test view不能處理這個事件,事件將沿這個view的響應者鏈條往下走,直到找到一個可以處理事件的對象。

 The Responder Chain Is Made Up of Responder Objects(響應者鏈條由響應者組成-。-)

許多類型的事件以來響應鏈條傳遞事件。響應者鏈條是一系列連起來的響應者對象。以第一響應者開始,結束語application對象。如果第一響應者不能處理一個事件,它就轉發給響應者鏈條中的下一個響應者。

responder object is an object that can respond to and handle events. The  UIResponder  class is the base class for all responder objects, and it defines the PRogrammatic interface not only for event handling but also for common responder behavior. Instances of the UIApplicationUIViewController, and UIView classes are responders, which means that all views and most key controller objects are responders. Note that Core Animation layers are not responders.

第一響應者被指定首先接受事件。通常,第一響應者是一個view對象。一個對象可以變成第一響應者通過做下面兩件事:

  1. 重寫 canBecomeFirstResponder 方法,返回YES;
  2. 接受一個 becomeFirstResponder 消息。如果必要,一個對象可以給自己發送這條消息。

依賴響應者鏈條的對象不止事件對象。響應者鏈條可以用在下面的地方:

  1. Touch events.
  2. Motion events.
  3. Remote control events.
  4. Action messages.
  5. Editing-menu messages.
  6. Text editing.

UIKit自動設置用戶點擊的text field 或 text view成為第一響應者;Apps必須顯式設置所有其他的第一響應者,用becomeFirstResponder方法。

 

更多更詳細內容:https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiphoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html

http://www.survivalescaperooms.com/xjshi/p/4356018.html


上一篇:iOS多線程總結

下一篇:掌握iOS自動布局

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青田县| 道真| 永川市| 汝阳县| 东乡| 资中县| 玛曲县| 临城县| 稻城县| 澎湖县| 新蔡县| 北碚区| 沙田区| 寻甸| 正阳县| 崇礼县| 祁连县| 同心县| 天水市| 灵川县| 广丰县| 高碑店市| 龙陵县| 安岳县| 龙海市| 家居| 商洛市| 商丘市| 城口县| 合山市| 大竹县| 曲靖市| 县级市| 唐海县| 南开区| 金溪县| 扶绥县| 浠水县| 祁阳县| 奉节县| 新巴尔虎左旗|