您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 技巧大全

iOS 七種手勢辨識完整整理:從 UIGestureRecognizer 到各子類

編輯:iOS 技巧大全

UIGestureRecognizer 是所有手勢辨識類別的基底類別,提供手勢辨識的基本能力。有了它之後,手勢的辨識過程全部由系統處理,我們只需要關心「辨識成功之後要做什麼」。

它的子類共有六個,加上平移手勢的子類「螢幕邊緣手勢」,一共七種:輕拍、長按、輕掃、平移、捏合、旋轉,以及螢幕邊緣平移。

通用三步驟

1. 建立手勢辨識物件(指定 target 與 action)
2. 設定相關屬性(需要幾根手指、需按壓多久、掃動方向等)
3. 把物件加到 view 上
4. 釋放物件(MRC 下)

一個 view 可以同時掛多個手勢,例如同時支援向左與向右輕掃,就建立兩個 UISwipeGestureRecognizer 分別設定方向。

完整程式碼

//
// GestureRecognizerViewController.m
//
#import "GestureRecognizerViewController.h"
#import "UIColor+RandomColor.h"

@interface GestureRecognizerViewController () {
    CGRect _frame;   // 用來記錄 view 原本的 frame
}
@end

@implementation GestureRecognizerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(60, 184, 200, 200)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    [view release];

    // ── 輕拍手勢(最常用,行為近似按鈕)──
    // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
    //     initWithTarget:self action:@selector(handleTapGesture:)];
    // tapGesture.numberOfTapsRequired   = 2;   // 必須在 addGestureRecognizer 之前設定
    // tapGesture.numberOfTouchesRequired = 2;
    // [view addGestureRecognizer:tapGesture];
    // [tapGesture release];

    // ── 長按手勢 ──
    // UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
    //     initWithTarget:self action:@selector(handleLongPressGesture:)];
    // longPressGesture.minimumPressDuration = 1;   // 最短按壓秒數
    // [view addGestureRecognizer:longPressGesture];
    // [longPressGesture release];

    // ── 輕掃手勢(預設向右)──
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
        initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
    [view addGestureRecognizer:swipeGesture];
    [swipeGesture release];

    // 再加一個支援向左輕掃
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
        initWithTarget:self action:@selector(handleSwipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [view addGestureRecognizer:swipe];
    [swipe release];

    // ── 平移手勢 ──
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
        initWithTarget:self action:@selector(handlePanGesTure:)];
    [view addGestureRecognizer:panGesture];
    [panGesture release];

    // ── 捏合手勢 ──
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
        initWithTarget:self action:@selector(handlePinchGesture:)];
    [view addGestureRecognizer:pinchGesture];
    [pinchGesture release];

    // ── 旋轉手勢 ──
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]
        initWithTarget:self action:@selector(handleRotationGesture:)];
    [view addGestureRecognizer:rotationGesture];
    [rotationGesture release];

    // ── 螢幕邊緣手勢(UIPanGestureRecognizer 的子類)──
    UIScreenEdgePanGestureRecognizer *screenEdgePanGesture =
        [[UIScreenEdgePanGestureRecognizer alloc]
            initWithTarget:self action:@selector(handleScreenEdgeGesture:)];
    screenEdgePanGesture.edges = UIRectEdgeRight;

    // 必須讓 view 位置與螢幕邊緣重疊,否則永遠觸發不到
    view.frame = CGRectMake(120, 50, 200, 200);

    // 記錄原始 frame,供手勢結束時復原
    _frame = view.frame;

    [view addSubview:screenEdgePanGesture];
    [screenEdgePanGesture release];
}

#pragma mark - 輕拍
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
    tapGesture.view.backgroundColor = [UIColor randomColor];
}

#pragma mark - 長按
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture {
    // Began:達到 minimumPressDuration 時觸發一次
    // Changed:達到條件後手指滑動時持續觸發
    // Ended:手指離開時觸發
    if (longPressGesture.state == UIGestureRecognizerStateEnded) {
        longPressGesture.view.superview.backgroundColor = [UIColor randomColor];
    }
}

#pragma mark - 輕掃
- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture {
    swipeGesture.view.backgroundColor = [UIColor randomColor];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
    swipe.view.backgroundColor = [UIColor randomColor];
}

#pragma mark - 平移
- (void)handlePanGesTure:(UIPanGestureRecognizer *)panGesture {
    // 1. 取得本次移動的增量
    CGPoint point = [panGesture translationInView:panGesture.view];

    // 2. 以「上次的 transform」為基準做位移(仿射變換)
    panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);

    // 3. 把增量歸零,下次才能拿到「這一次的」位移量
    [panGesture setTranslation:CGPointZero inView:panGesture.view];

    panGesture.view.backgroundColor = [UIColor randomColor];
}

#pragma mark - 捏合
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture {
    // 以「上次的 transform」為基準做縮放
    pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform,
                                                        pinchGesture.scale,
                                                        pinchGesture.scale);
    // 縮放比例歸零(設為 1)
    pinchGesture.scale = 1;

    pinchGesture.view.backgroundColor = [UIColor randomColor];
}

#pragma mark - 旋轉
- (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture {
    // 以「上次的 transform」為基準做旋轉
    rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform,
                                                            rotationGesture.rotation);
    // 角度歸零
    rotationGesture.rotation = 0;

    rotationGesture.view.backgroundColor = [UIColor randomColor];
}

#pragma mark - 螢幕邊緣
- (void)handleScreenEdgeGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgeGesture {
    // 手指離開時回到原始位置
    if (screenEdgeGesture.state == UIGestureRecognizerStateEnded) {
        screenEdgeGesture.view.frame = _frame;
    }

    CGPoint point = [screenEdgeGesture translationInView:screenEdgeGesture.view];
    screenEdgeGesture.view.transform = CGAffineTransformTranslate(screenEdgeGesture.view.transform,
                                                                 point.x, point.y);
    [screenEdgeGesture setTranslation:CGPointZero inView:screenEdgeGesture.view];
}

@end

隨機色分類

// UIColor+RandomColor.h
#import <UIKit/UIKit.h>

@interface UIColor (RandomColor)
+ (UIColor *)randomColor;
@end
// UIColor+RandomColor.m
#import "UIColor+RandomColor.h"

#define kColorValue arc4random_uniform(256) / 255.0

@implementation UIColor (RandomColor)

+ (UIColor *)randomColor {
    return [UIColor colorWithRed:kColorValue
                          green:kColorValue
                           blue:kColorValue
                          alpha:kColorValue];
}

@end

幾個容易出錯的地方

位移、縮放、旋轉都必須「歸零」。這是平移/捏合/旋轉三個手勢最關鍵的觀念。translationInView:、scale、rotation 回傳的都是「自上次讀取以來的增量」,如果不呼叫 setTranslation:CGPointZero 或把值設回 1/0,下一次拿到的就是累加值,view 會跑得太快。

用 transform 而非 frame。transform 只觸發合成不觸發 layout,效能更好;而且以「上次的 transform」為基準串接,天然支援連續疊加。

邊緣手勢的 view 必須貼齊螢幕邊緣。範例中把 view 從 x = 60 改成 x = 120 這步很容易被忽略。系統判斷手勢是否從邊緣起手,是依觸碰座標是否落在 edges 指定的區域內,view 沒貼邊就永遠不會觸發。

長按手勢一定要判斷 state。它的 action 會被連續呼叫很多次,不做 state 過濾會導致同一段邏輯被執行數十次。

手勢衝突。平移與螢幕邊緣平移、輕拍與長按都可能互相搶事件。需要時可實作 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 或 gestureRecognizerShouldBegin: 來協調。

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