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

iOS 六類手勢辨識器全解析與實作範例

編輯:iOS 7 技巧

手勢操作是 iOS 互動的核心。UIKit 內建六種 UIGestureRecognizer 子類,涵蓋了大部分常見需求。以下逐一說明各自的關鍵屬性與注意事項。

一、手勢種類

  • UITapGestureRecognizer:敲擊、點擊
  • UILongPressGestureRecognizer:長按
  • UIPinchGestureRecognizer:縮放
  • UIRotationGestureRecognizer:旋轉
  • UISwipeGestureRecognizer:輕掃
  • UIPanGestureRecognizer:拖拽

二、代理方法(UIGestureRecognizerDelegate)

手勢是否要開始,決定它會不會生效;回傳 NO 可以阻止手勢發生:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

是否允許兩個手勢同時發生:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

第二個方法在多手勢並存時特別重要——例如同時要支援縮放與拖拽,就必須回傳 YES。

三、UITapGestureRecognizer 點擊

  • numberOfTapsRequired:需要點擊幾次才觸發(單擊、雙擊)。
  • numberOfTouchesRequired:需要幾根手指同時點擊。
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.delegate = self;
tap.numberOfTapsRequired = 1;    // 設置點擊次數觸發手勢事件
tap.numberOfTouchesRequired = 1; // 設置需要點擊的手指數
[self.image addGestureRecognizer:tap];

四、UILongPressGestureRecognizer 長按

  • minimumPressDuration:手指按下到離開的最小間隔,短於此值不算長按。
  • allowableMovement:長按期間允許移動的像素範圍,超過就取消。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
longPress.delegate = self;
longPress.minimumPressDuration = 1.0;  // 手指按下與離開間隔時間
longPress.allowableMovement = 30;      // 按下過程中允許移動的像素
[self.image addGestureRecognizer:longPress];

五、UIPinchGestureRecognizer 縮放

scale 是相對於原始大小的縮放比例。

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate = self;
pinch.scale = 1.2;
[self.image addGestureRecognizer:pinch];

六、UIRotationGestureRecognizer 旋轉

rotation 是旋轉弧度。這裡有個關鍵:每次手勢結束後必須把它歸零,否則第二次旋轉會從初始位置重新計算,畫面看起來就會跳。

- (void)setupRotation {
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    rotation.delegate = self;
    [self.image addGestureRecognizer:rotation];
}

- (void)rotation:(UIRotationGestureRecognizer *)rotation {
    // 旋轉角度
    CGFloat radian = rotation.rotation;
    self.image.transform = CGAffineTransformRotate(self.image.transform, radian);
    // 復位,保證每次都是在上一次位置開始轉
    rotation.rotation = 0;
}

七、UISwipeGestureRecognizer 輕掃

手指按下後在螢幕上滑動。分上下左右四個方向,每個方向必須各自建立一個物件——一個 swipe 實例只能對應一個方向。

typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
    UISwipeGestureRecognizerDirectionRight = 1 << 0, // 從左向右
    UISwipeGestureRecognizerDirectionLeft  = 1 << 1, // 從右向左
    UISwipeGestureRecognizerDirectionUp    = 1 << 2, // 從下往上
    UISwipeGestureRecognizerDirectionDown  = 1 << 3  // 從上往下
};

UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeUp.delegate = self;
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;   // 從下往上
[self.image addGestureRecognizer:swipeUp];

UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown; // 從上往下
[self.image addGestureRecognizer:swipeDown];

八、UIPanGestureRecognizer 拖拽

按下後拖動物件。這裡最容易搞混的是兩個座標方法的差異:

  • locationInView: 手指在該 view 中的實際座標(絕對位置)。
  • translationInView: 本次手勢的位移量,是相對於父 view 的增量。
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
pan.delegate = self;
[self.image addGestureRecognizer:pan];

// 手勢的觸摸點
// CGPoint p = [pan locationInView:self.image];

// 手勢的移動點(每次移動的位移點)
CGPoint transP = [pan translationInView:self.image];
NSLog(@"%f, %f", transP.x, transP.y);
self.image.transform = CGAffineTransformTranslate(self.image.transform, transP.x, transP.y);

// 復位
[pan setTranslation:CGPointZero inView:self.image];

最後那行 setTranslation:CGPointZero 非常重要:位移量是「自上次復位以來的累積值」,不歸零的話物件會以加速度方式飛出畫面。

實作上的共同原則

  1. 手勢只能加在 isUserInteractionEnabled = YES 的 view 上;UIImageView 預設是 NO,必須手動打開,這是最常見的「手勢沒反應」原因。
  2. 需要「位移量」的手勢(pan、pinch、rotation)都要在每次回呼結尾復位,改成增量式處理。
  3. 多個手勢並存時記得實作 shouldRecognizeSimultaneously 代理方法。
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved