Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

iOS UI 進階:觸摸事件與手勢識別完全梳理

編輯:關於 Android 程式設計

一、觸摸事件的處理方式

iOS 上的事件大致分三類:觸摸事件(手指觸碰屏幕)、運動事件(如微信搖一搖)、遠程控制事件(如藍牙耳機的按鍵)。本文聚焦第一類。

響應者對象通過覆寫以下四個方法來接收觸摸:

// 一或多個手指開始觸碰屏幕
- (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;

UITouch 觸摸對象

當用戶觸摸視圖時,可以在 touchesBegan 中取出 UITouch 對象,進而判斷點擊次數:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSInteger tapNumber = [touch tapCount];
}

用戶觸摸屏幕時,系統會把事件封裝成一個 UIEvent 實例,其中包含若干 UITouch 實例,一個 UITouch 對應一根手指。

UITouch 的常用屬性:

window:觸摸所處的窗口;view:觸摸發生時所在的視圖。

tapCount:短時間內輕擊屏幕的次數,可據此判斷單擊、雙擊或多次點擊。

phase:觸摸在屏幕上的一個生命週期,用來查詢當前所處狀態,枚舉值包括:

UITouchPhaseBegan       觸摸開始
UITouchPhaseMoved       接觸點移動
UITouchPhaseStationary  接觸點未移動
UITouchPhaseEnded       觸摸結束
UITouchPhaseCancelled   觸摸被取消

另外兩個常用的座標方法:

// 返回觸摸點在指定 view 坐標系中的位置
- (CGPoint)locationInView:(UIView *)view;

// 返回上一個觸摸點的座標
- (CGPoint)previousLocationInView:(UIView *)view;

二、事件傳遞的過程

從事件發生到最終被處理,中間要經過一段標準化的流程。當用戶點擊屏幕時,iOS 捕獲這一連串觸摸,打包成 UIEvent 對象並放入應用程序的活動事件隊列。UIApplication 從隊列中取出最先進來的事件並開始分發:通常先交給應用的主窗口(UIWindow),再由窗口轉交給「第一響應者」——也就是被觸摸的那個視圖。

三、響應者鏈

響應者對象是能接收並處理事件的對象,UIResponder 是所有響應者的基類,它既提供事件處理接口,也提供通用的響應行為。UIApplication、UIView(含 UIWindow)、UIViewController 都直接或間接繼承自 UIResponder,因此它們的實例都是響應者。

事件沿響應者鏈向上傳遞的規則是:

用戶與視圖交互 → 事件先傳給視圖控制器;若無控制器,則傳給父視圖;若當前對象不處理,繼續向上傳遞;最頂層視圖仍不處理,交給 UIWindow;最後交給 UIApplication;若依然無人處理,事件被丟棄。

事件分發與響應的完整流程:

如何攔截事件、讓視圖不再參與響應鏈:

userInteractionEnabled = NO;
hidden = YES;
alpha = 0 ~ 0.01   // 幾乎透明時 ALSO 不響應觸摸

四、手勢識別

UIGestureRecognizer 是一個抽象類,定義了所有手勢的基本行為,專門用來檢測和識別用戶手勢。它的常用子類分工明確:

UITapGestureRecognizer          輕擊
UIPinchGestureRecognizer        捏合
UIPanGestureRecognizer          平移
UISwipeGestureRecognizer        輕掃
UIRotationGestureRecognizer     旋轉
UILongPressGestureRecognizer    長按

建立並添加手勢識別器的標準寫法:

// 1. 單擊
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[imageV addGestureRecognizer:tap];

// 2. 雙擊
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[imageV addGestureRecognizer:doubleTap];
// 雙擊失敗後才算單擊
[tap requireGestureRecognizerToFail:doubleTap];

// 3. 長按,allowableMovement 允許手指抖動的範圍
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[imageV addGestureRecognizer:longPress];

// 4. 輕掃,默認方向為右
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// swipe.direction = UISwipeGestureRecognizerDirectionRight;
[imageV addGestureRecognizer:swipe];

// 5. 平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
// 輕掃與平移衝突時默認平移優先,可讓輕掃先失敗
[pan requireGestureRecognizerToFail:swipe];
[imageV addGestureRecognizer:pan];

// 6. 旋轉
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageV addGestureRecognizer:rotation];

// 7. 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageV addGestureRecognizer:pinch];

各手勢的回调實現

// 捏合:scale 為縮放比例,velocity 為縮放速度
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    if (pinch.state == UIGestureRecognizerStateChanged) {
        CGFloat pScale = pinch.scale;
        pinch.view.transform = CGAffineTransformMakeScale(pScale, pScale);
    } else if (pinch.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.2 animations:^{
            pinch.view.transform = CGAffineTransformIdentity;
        }];
    }
}

// 旋轉:rotation 為弧度(角度 0~360 對應弧度 0~2π),velocity 為旋轉速度
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
    if (rotation.state == UIGestureRecognizerStateChanged) {
        CGFloat angle = rotation.rotation;
        rotation.view.transform = CGAffineTransformMakeRotation(angle);
    } else if (rotation.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.2 animations:^{
            rotation.view.transform = CGAffineTransformIdentity;
        }];
    }
}

// 平移:獲取相對於起始點的位移與速度,也可反向設置位移
- (void)panAction:(UIPanGestureRecognizer *)pan {
    CGPoint p1 = [pan translationInView:imageV];
    CGPoint v1 = [pan velocityInView:imageV];
    // CGPoint p2 = CGPointMake(100, 100);
    // [pan setTranslation:p2 inView:imageV];
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe { /* 輕掃 */ }

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"長按開始");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"長按結束");
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap { NSLog(@"單擊"); }
- (void)doubleTapAction:(UITapGestureRecognizer *)tap { NSLog(@"雙擊"); }

把這幾塊串起來看:UITouch 負責「原始座標」,UIGestureRecognizer 負責「語義化手勢」,響應者鏈負責「誰來處理」。三者分工清楚,多數交互需求都能拆解到這三層裡去實現。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved