您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 手勢識別實戰:輕按、輕掃、張合與旋轉

編輯:iOS 相關資訊

Apple 為各類手勢提供了對應的識別器類別。本文用四個最常見的識別器做示例,每個手勢都配一個彈窗回饋,方便直觀確認效果。搖動(shake)不在本文範圍內。

先記住一個通用套路,四種手勢都是照著它走的:創建識別器實例 → 初始化並綁定 action → 配置屬性 → 添加到視圖。

如果想獲取手勢發生位置的坐標,可以這樣寫:

CGPoint location = [recognizer locationInView:self.view];
// location.x / location.y

一、輕按 UITapGestureRecognizer

回調方法:

- (void)foundTap:(UITapGestureRecognizer *)recognizer {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
        message:@"tapped" delegate:nil
        cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

在 viewDidLoad 中註冊:

UITapGestureRecognizer *tapRecognizer =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(foundTap:)];
tapRecognizer.numberOfTapsRequired   = 1;   // 點擊次數
tapRecognizer.numberOfTouchesRequired = 1;  // 需要幾根手指
[self.view addGestureRecognizer:tapRecognizer];

二、輕掃 UISwipeGestureRecognizer

回調方法:

- (void)foundSwipe:(UISwipeGestureRecognizer *)recognizer {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
        message:@"swiped" delegate:nil
        cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

註冊時需要指定方向,四個方向可以按需組合:

UISwipeGestureRecognizer *swipeRecognizer =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(foundSwipe:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight
                          | UISwipeGestureRecognizerDirectionLeft;
swipeRecognizer.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:swipeRecognizer];

可選方向為 Right / Left / Up / Down。注意與 Tap 不同,一個 Swipe 識別器只能觸發一次,因此如需同時響應多個方向,通常會為每個方向各建一個實例。

三、張合 UIPinchGestureRecognizer

用於實現放大縮小。回調方法:

- (void)foundPinch:(UIPinchGestureRecognizer *)recognizer {
    double scale = recognizer.scale;   // 縮放比例
    // 可結合 OpenGL ES 的 glScalef 實現實際縮放

    NSString *message = [NSString stringWithFormat:
        @"縮放:Scale:%1.2f, Velocity:%1.2f",
        recognizer.scale, recognizer.velocity];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
        message:message delegate:nil
        cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

註冊:

UIPinchGestureRecognizer *pinchRecognizer =
    [[UIPinchGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(foundPinch)];
[self.view addGestureRecognizer:pinchRecognizer];

scale 是相對於手勢起點的倍數,velocity 則是變化速度,做跟手動畫時常用它來判斷方向與力度。

四、旋轉 UIRotationGestureRecognizer

這裡有個容易踩的坑:Cocoa 系框架一律使用弧度作為角度單位,換算關係是:

角度 = 弧度 * 180 / M_PI

回調方法:

- (void)foundRotation:(UIRotationGestureRecognizer *)recognizer {
    double rotation = recognizer.rotation;   // 弧度
    // 可結合 glRotatef 實現實際旋轉

    NSString *message = [NSString stringWithFormat:
        @"旋轉,Radians:%1.2f, Velocity:%1.2f",
        recognizer.rotation, recognizer.velocity];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello"
        message:message delegate:nil
        cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

註冊:

UIRotationGestureRecognizer *rotationRecognizer =
    [[UIRotationGestureRecognizer alloc] initWithTarget:self
                                                 action:@selector(foundRotation:)];
[self.view addGestureRecognizer:rotationRecognizer];

五、實戰中的幾點經驗

  • 多個識別器共存時,難免會互相搶事件。這時應該實現 UIGestureRecognizerDelegate 的 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 返回 YES,允許並發識別。

  • 如果手勢需要與 UIScrollView 共存(例如全屏滑動翻頁),可以通過 gestureRecognizer:shouldReceiveTouch: 過濾掉落在特定子視圖上的觸摸。

  • 本文示例中的 UIAlertView 在 iOS 7 之後已建議改用 UIAlertController;手勢演示時更推薦直接改動一個 label 或 imageView 的屬性,回饋更直觀也不打斷操作。

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