Event Delivery: The Responder Chain(事件傳遞,響應鏈),deliveryresponder

來源:互聯網
上載者:User

Event Delivery: The Responder Chain(事件傳遞,響應鏈),deliveryresponder

當我們設計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:

 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對象。一個對象可以變成第一響應者通過做下面兩件事:

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

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.cnblogs.com/xjshi/p/4356018.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.