iOS 中的事件主要分為三大類:觸摸事件、加速計事件與遠程控制事件。
並非任何物件都能處理事件。只有繼承自 UIResponder 的物件才能接收並處理事件,我們稱之為「回應者物件」。UIApplication、UIViewController、UIView 都繼承自 UIResponder,因此三者都是回應者物件。
// 下一個回應者 - (nullable UIResponder *)nextResponder; // 第一回應者相關 - (BOOL)canBecomeFirstResponder; - (BOOL)becomeFirstResponder; - (BOOL)canResignFirstResponder; - (BOOL)resignFirstResponder; - (BOOL)isFirstResponder; // 觸摸事件(需先呼叫 setMultipleTouchEnabled:YES 才會收到多點觸摸) - (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event; - (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event; - (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event; - (void)touchesCancelled:(nullable NSSet *)touches withEvent:(nullable UIEvent *)event; // 按壓事件(多用於遙控器) - (void)pressesBegan:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event; - (void)pressesChanged:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event; - (void)pressesEnded:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event; - (void)pressesCancelled:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event; // 加速計事件(例如微信的搖一搖) - (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event; - (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event; - (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event; // 遠程控制事件(多用於耳機線控) - (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event;
UITouch 同樣繼承自 NSObject。關於它有幾個重要特性:
@property(nonatomic, readonly) NSTimeInterval timestamp; // 產生或變化的時間 @property(nonatomic, readonly) UITouchPhase phase; // 當前所處階段 @property(nonatomic, readonly) NSUInteger tapCount; // 短時間內點擊次數 @property(nonatomic, readonly) UITouchType type; @property(nullable, nonatomic, readonly, strong) UIWindow *window; @property(nullable, nonatomic, readonly, strong) UIView *view; @property(nullable, nonatomic, readonly, copy) NSArray *gestureRecognizers; // 觸摸點在指定 view 座標系中的位置;傳 nil 則回傳視窗座標 - (CGPoint)locationInView:(nullable UIView *)view; // 上一個觸摸點的位置 - (CGPoint)previousLocationInView:(nullable UIView *)view;
UIEvent 一樣繼承自 NSObject。要注意的是:每產生一個觸摸事件就會產生一個 UIEvent 物件;而按壓事件對應的是其子類 UIPressesEvent,加速計事件與遠程控制事件則對應 UIEvent。
@property(nonatomic, readonly) UIEventType type; @property(nonatomic, readonly) UIEventSubtype subtype; @property(nonatomic, readonly) NSTimeInterval timestamp; - (nullable NSSet *)allTouches; - (nullable NSSet *)touchesForWindow:(UIWindow *)window; - (nullable NSSet *)touchesForView:(UIView *)view; - (nullable NSSet *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture;
當使用者與觸摸屏互動時,硬體會偵測到物理接觸並通知作業系統;作業系統接著建立對應事件,把它放進目前執行中的應用程式的事件佇列;事件循環再把事件交給優先回應者——也就是事件觸發時與使用者互動的那個控件,例如按鈕或視圖。若我們有實作該類型的處理方法,它就會被執行;以觸摸事件為例,就是覆寫那四個 touches 方法。
處理完之後,回應者有兩個選擇:丟棄,或沿著回應者鏈傳給下一個回應者。下一個回應者的位置就存在目前的 nextResponder 變數中。事件會一路往傳,直到遇到能處理它的物件,或抵達鏈條末端也就是 UIApplication;UIApplication 收到後同樣只能處理或丟棄。
更具體地說:
1. 觸摸發生後,系統把事件加入由 UIApplication 管理的事件佇列。UIApplication 從佇列取出最前面的事件並向下分發,通常是先送給主視圖(keyWindow),主視圖再於視圖層級結構中找到最適合的視圖來處理,隨後呼叫該視圖的 touches 方法。請注意:如果父控件無法接收觸摸事件,子控件也不可能收到。
2. 那麼「最適合的控件」怎麼找?依序判斷:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
// 1. 能否處理事件
if (self.userInteractionEnabled == NO || self.alpha <= 0.01 || self.hidden == YES) {
return nil;
}
// 2. 觸摸點是否在自己身上
if ([self pointInside:point withEvent:event] == NO) {
return nil;
}
// 3. 從後往前遍歷子控件
NSInteger count = self.subviews.count;
for (NSInteger i = count - 1; i >= 0; i--) {
UIView *childView = self.subviews[i];
CGPoint childPoint = [self convertPoint:point toView:childView];
UIView *view = [childView hitTest:childPoint withEvent:event];
if (view) { return view; } // 子控件更合適
}
// 4. 沒有更合適的,回傳自己
return self;
}
一次完整的觸摸會經歷三個狀態,可能經歷第四個:
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event; // 開始 - (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event; // 移動 - (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event; // 結束 - (void)touchesCancelled:(nullable NSSet *)touches withEvent:(nullable UIEvent *)event; // 取消
幾個實務上很常考 also 很容易搞錯的細節:
回應者鏈是由多個回應者物件串接而成的鏈條。它的價值在於清楚呈現各回應者之間的關係,並且讓同一個事件能被多個物件依序處理。
如何判斷上一個回應者?
事件在鏈上的傳遞順序如下: