您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

IT English 精讀(1):iOS 的事件與觸摸處理

編輯:iOS 開發綜合

本篇單字與內容取自 Xcode 文件的 Event(iOS)一文,主題是使用者的手勢與觸摸事件。

原文

Events in iOS represent fingers touching views of an application or the user shaking the device. One or more fingers touch down on one or more views, perhaps move around, and then lift from the view or views. As this is happening, iPhone's Multi-Touch system registers these touches as events and sends them to the currently active application for processing. This range of possible touch behaviors, from the moment the first finger touches down on a view until the last finger lifts from that view, defines a multitouch sequence. Applications (and framework objects) analyze multitouch sequences, usually to determine whether they are gestures such as pinches and swipes. The operating system may also generate motion events when users shake the devices. These are delivered as discrete events to an application.

中文理解

iOS 的事件,指的是手指觸碰 App 的 view,或使用者晃動裝置。一根或多根手指落在同一或多個 view 上,可能移動,然後離開。過程中,iPhone 的多點觸控系统把這些觸碰記錄成事件,送給目前作用中的 App 處理。從第一根手指落下到最後一根離開,這整段可能的觸碰行為就構成一個「多點觸控序列」。App 與框架物件會分析這些序列,通常為了判斷它是否構成某種手势,例如縮放或滑動。此外,當使用者晃動裝置時,系統也會產生動作事件,以離散事件的形式送達 App。

以物件代表手指的觸碰

手指觸碰 view 由 UITouch 物件代表,其中包含:手指所在的 view、手指在該 view 中的位置、時間戳記,以及 phase(階段)。在一次多點觸控序列中,touch 物件會依固定順序經歷幾個階段:

  • UITouchPhaseBegan:手指剛接觸 view。
  • UITouchPhaseMoved:手指在該 view 或相鄰 view 上移動。
  • UITouchPhaseEnded:手指離開 view。

此外還有 stationary(靜止)與 cancelled(被取消)兩種狀態。touch 物件在整個序列中是持續存在的(persistent),但狀態會變化(mutates)。App 在把事件交給 view 處理時,會將 touch 物件打包進 UIEvent 物件中。

觸合物件的投遞路徑

在主事件循環中,應用程式物件從事件佇列取得原始觸碰事件,打包成 UIEvent 中的 UITouch,送到觸碰發生的那個 view 所屬的視窗;視窗再轉給該 view,也就是 hit-test view。若這個 view 無法處理(通常是因為沒實作必要的事件處理方法),事件會沿著 responder chain 往上傳,直到被處理或被丟棄。

處理事件必須覆寫的四個方法

包含自訂 view 與 view controller 在內的 responder 物件,透過 UIResponder 宣告的四個方法來處理事件:

  • touchesBegan:withEvent::處理 Began 階段的 touch。
  • touchesMoved:withEvent::處理 Moved 階段。
  • touchesEnded:withEvent::處理 Ended 階段。
  • touchesCancelled:withEvent::當外部事件(例如來電)使系統取消這次多點觸控序列時呼叫。

每個方法的第一個參數是該階段的 touch 物件集合,第二個參數則是追蹤整個序列中所有 touch 的 UIEvent。view 預設能接收多點觸控事件,但部分 UIKit view 類別因為 userInteractionEnabled 設為 NO 而無法接收;若繼承這些類別又需要事件,記得把該屬性設為 YES。自訂 view 與 view controller 建議四個方法都實作;若只是繼承 UIKit view 類別,則只需實作有興趣的階段,但此時務必先呼叫 superclass 的實作。

透過動作事件處理晃動

系統透過裝置的加速計偵測特定動作並以事件送出(目前僅支援晃動裝置)。first responder 會先收到這些動作事件(以 UIEvent 形式),若無法處理則沿 responder chain 往上傳。系統只會告知動作開始與結束,對應的 UIResponder 方法是 motionBegan:withEvent: 與 motionEnded:withEvent:。傳給處理者的資訊僅包含事件物件所封裝的內容:事件類型(UIEventTypeMotion)、子類型(UIEventSubtypeMotionShake)與時間戳記。

生詞

  • lift from 從…抬起
  • register [ˈredʒɪstə] 記錄、登錄
  • process [prəˈses] 加工、處理
  • range [reɪndʒ] 範圍
  • feature [ˈfiːtʃə] 特徵
  • determine [dɪˈtɜːmɪn] 決定
  • pinch [pɪntʃ] 捏
  • swipe [swaɪp] 滑動
  • motion [ˈməʊʃn] 動作
  • deliver [dɪˈlɪvə] 傳送
  • discrete [dɪˈskriːt] 不連續的、離散的
  • represent [ˌreprɪˈzent] 代表
  • phase [feɪz] 相位、階段
  • adjacent [əˈdʒeɪsnt] 鄰近的
  • raw [rɔː] 生的、未加工的
  • in turn 輪流、接著
  • hit-test 點擊測試
  • requisite [ˈrekwɪzɪt] 必備的、需要的
  • discard [dɪsˈkɑːd] 拋棄、丟棄
  • external [ɪkˈstɜːnl] 外部的
  • track [træk] 追蹤
  • corresponding [ˌkɒrɪˈspɒndɪŋ] 相應的
  • accelerometer [ˌækseləˈrɒmɪtə] 加速計
  • detect [dɪˈtekt] 偵測
  • encapsulate [ɪnˈkæpsjuleɪt] 封裝
  • stationary [ˈsteɪʃənəri] 靜止的
  • mutate [mjuːˈteɪt] 變化、突變
  • persistent [pəˈsɪstənt] 持續的
  • interaction [ˌɪntərˈækʃn] 互動

結語

以上是本篇全部內容。把「UITouch 的四個階段」與「四個 touches 方法」對應起來記,觸摸處理就不容易搞混。希望對大家有幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved