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

iOS 開發:響應者鏈與觸摸事件處理

編輯:iOS 開發綜合

觸摸事件的分類

使用者操作 App 的過程中會產生各式各樣的事件,iOS 中主要分為三大類:

觸摸事件
加速計事件
遠程控制事件

什麼是響應者物件

並非任何物件都能處理事件,只有繼承自 UIResponder 的類別才行,我們稱之為「響應者物件」。UIApplication、UIViewController、UIView 都繼承自 UIResponder,因此三者都能接收並處理事件。

UIResponder 提供的方法

// 觸摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

// 加速計事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

// 遠程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

UIView 的觸摸事件處理

UIView 是 UIResponder 的子類,可以覆蓋四個方法來回應不同階段:

// 一根或多根手指開始觸摸 view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

// 手指在 view 上移動,隨移動持續呼叫
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

// 手指離開 view
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

// 觸摸過程被系統事件(例如來電)打斷
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

提示:touches 集合中存放的都是 UITouch 物件。

UITouch

使用者用一根手指觸摸螢幕時,系統會建立一個與該手指關聯的 UITouch 物件,一根手指對應一個 UITouch。它保存著與手指相關的資訊,例如觸摸位置、時間與階段。

手指移動時,系統會更新同一個 UITouch 物件,使其持續記錄最新座標;手指離開螢幕時,對應的 UITouch 物件隨之銷毀。

常用屬性:

// 觸摸產生時所處的視窗
@property(nonatomic, readonly, retain) UIWindow *window;

// 觸摸產生時所處的視圖
@property(nonatomic, readonly, retain) UIView *view;

// 短時間內點按螢幕的次數,可用來判斷單擊、雙擊或更多次點擊
@property(nonatomic, readonly) NSUInteger tapCount;

// 記錄觸摸事件產生或變化的時間,單位為秒
@property(nonatomic, readonly) NSTimeInterval timestamp;

// 當前觸摸事件所處的狀態
@property(nonatomic, readonly) UITouchPhase phase;

常用方法:

// 回傳觸摸在 view 上的位置,
// 座標系以 view 左上角為原點 (0, 0);
// 若傳入 nil,回傳的是觸摸點在 UIWindow 中的位置
- (CGPoint)locationInView:(UIView *)view;

// 該方法記錄了前一個觸摸點的位置
- (CGPoint)previousLocationInView:(UIView *)view;

UIEvent

每產生一個事件,就會產生一個 UIEvent 物件,用來記錄事件產生的時刻與類型。

// 事件類型
@property(nonatomic, readonly) UIEventType type;
@property(nonatomic, readonly) UIEventSubtype subtype;

// 事件產生的時間
@property(nonatomic, readonly) NSTimeInterval timestamp;

touches 與 event 兩個參數的含義

一次完整的觸摸過程會經歷三到四個階段:開始 → 移動 → 結束,或開始 → 移動 → 取消。四個方法都有 touches 與 event 兩個參數,其中有幾個規律值得記住:

一次完整的觸摸過程只會產生一個事件物件,也就是說四個方法中收到的 event 是同一個。若兩根手指同時觸摸同一個 view,view 只會呼叫一次 touchesBegan:withEvent:,此時 touches 中有兩個 UITouch;若兩根手指前後分開觸摸,則會分別呼叫兩次,每次 touches 中只有一個 UITouch。因此,判斷 touches 中 UITouch 的數量,就能分辨單點與多點觸摸。

觸摸事件實例:拖動一個視圖

#import "TouchView.h"

@implementation TouchView

// 一個完整的觸摸過程:touchesBegan -> touchesMoved -> touchesEnded

// 觸摸開始(手指剛接觸到 view)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSLog(@"touchesBegan--%d", touches.count);
    NSLog(@"%d", touch.tapCount);
}

// 觸摸中(手指在 view 上移動)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    CGPoint current = [touch locationInView:self];   // 當前觸摸點
    CGPoint previous = [touch previousLocationInView:self]; // 上一個觸摸點

    // 依位移量修改 view 的中心點
    CGPoint center = self.center;
    center.x += current.x - previous.x;
    center.y += current.y - previous.y;
    self.center = center;
}

// 觸摸結束(手指離開 view)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // UITouch *touch = [touches anyObject];
    // NSLog(@"touchesEnded--%d", touches.count);
}

@end

這裡的關鍵技巧是用「當前位置減去上一個位置」得到位移量,再累加到中心點上。若改用 current 直接指定中心,視圖會瞬間跳到手指位置,體驗很差。

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