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

iOS 手勢辨識完全指南:六種 UIGestureRecognizer 與實作範例

編輯:iOS 開發綜合

做過觸控互動的人都懂,自己用 touchesBegan: 去算位移、判斷點擊次數是多麼痛苦。UIKit 早就把這件事包好了: UIGestureRecognizer 負責偵測裝置上發生手勢,它本身是抽象類別,定義了所有手勢的共同行為,再往下派生出六個具體子類。

六種內建手勢

  • UITapGestureRecognizer:拍擊,可指定次數與手指數量
  • UIPinchGestureRecognizer:兩指捏合或張開,常用於縮放
  • UIPanGestureRecognizer:拖曳或搖動
  • UISwipeGestureRecognizer:單方向的快速掃動
  • UIRotationGestureRecognizer:兩指向相反方向轉動
  • UILongPressGestureRecognizer:長按

每一種識別器都有自己的可設定屬性,例如拍擊手勢可以設定需要幾次點擊、幾根手指;手勢成立後會向 target 送出 action 訊息,不同識別器帶回的參數也不同。

單指點兩下

UITapGestureRecognizer *oneFingerTwoTaps =
    [[[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(oneFingerTwoTaps)] autorelease];
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];
[[self view] addGestureRecognizer:oneFingerTwoTaps];

- (void)oneFingerTwoTaps
{
    NSLog(@"Action: One finger, two taps");
}

雙指點兩下

UITapGestureRecognizer *twoFingersTwoTaps =
    [[[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(twoFingersTwoTaps)] autorelease];
[twoFingersTwoTaps setNumberOfTapsRequired:2];
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:twoFingersTwoTaps];

- (void)twoFingersTwoTaps
{
    NSLog(@"Action: Two fingers, two taps");
}

上下掃動並取得起始座標

// 向上掃
UISwipeGestureRecognizer *oneFingerSwipeUp =
    [[[UISwipeGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(oneFingerSwipeUp:)] autorelease];
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[[self view] addGestureRecognizer:oneFingerSwipeUp];

- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationInView:[self view]];
    NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
}

// 向下掃
UISwipeGestureRecognizer *oneFingerSwipeDown =
    [[[UISwipeGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(oneFingerSwipeDown:)] autorelease];
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[[self view] addGestureRecognizer:oneFingerSwipeDown];

- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationInView:[self view]];
    NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
}

注意 UISwipeGestureRecognizer 一次只能認一個方向,若要同時支援上下左右,得分別建立四個實例。

旋轉手勢

UIRotationGestureRecognizer *twoFingersRotate =
    [[[UIRotationGestureRecognizer alloc] initWithTarget:self
                                                 action:@selector(twoFingersRotate:)] autorelease];
[[self view] addGestureRecognizer:twoFingersRotate];

- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
{
    // 把弧度換算成角度,比較直覺
    NSLog(@"Rotation in degrees since last change: %f",
          [recognizer rotation] * (180 / M_PI));
}

回傳的 rotation 是「相對上一次變更」的弧度值,因此每個回呼都要重新套用到檢視的 transform 上,才會有連續旋轉的效果。

捏合縮放

UIPinchGestureRecognizer *twoFingerPinch =
    [[[UIPinchGestureRecognizer alloc] initWithTarget:self
                                             action:@selector(twoFingerPinch:)] autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    NSLog(@"Pinch scale: %f", recognizer.scale);
}

最後補一個實務觀念:多個手勢同時疊在同一個檢視上時,難免互相搶事件。此時可實作 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 允許並存,或用 requireGestureRecognizerToFail: 建立優先順序,避免手勢打架。

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