您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 程式設計技術

iOS 六大手勢辨識器完整範例整理

編輯:iOS 程式設計技術

iOS 共提供六種標準手勢辨識器:

  • 輕點 UITapGestureRecognizer
  • 輕掃 UISwipeGestureRecognizer
  • 長按 UILongPressGestureRecognizer
  • 拖動 UIPanGestureRecognizer
  • 捏合 UIPinchGestureRecognizer
  • 旋轉 UIRotationGestureRecognizer

一、輕點手勢

UITapGestureRecognizer *tap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
tap.numberOfTapsRequired = 1;   // 點擊次數
tap.numberOfTouchesRequired = 1; // 手指數量
[self.view addGestureRecognizer:tap];

- (void)tapAction:(UITapGestureRecognizer *)sender {
    // 點擊次數與手指數可從 sender 讀取
}

二、長按手勢

UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 0.5;   // 最少按壓秒數
[self.view addGestureRecognizer:longPress];

- (void)longPressAction:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        // 開始長按
    }
}

長按的常用狀態:

UIGestureRecognizerStateBegan  開始
UIGestureRecognizerStateChanged 進行中(長按會持續回呼,適合拖放)
UIGestureRecognizerStateEnded  結束
UIGestureRecognizerStateCancelled 取消
UIGestureRecognizerStateFailed  失敗

三、輕掃手勢

每個辨識器實例只對應一個方向,要同時支援左右需各建一個。

UISwipeGestureRecognizer *swipeRight =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];

UISwipeGestureRecognizer *swipeLeft =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];

- (void)swipeAction:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        // 向左
    } else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        // 向右
    }
}

四、捏合手勢

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

- (void)pinchAction:(UIPinchGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateChanged) {
        // scale 為相對起點的縮放比例
        _imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
    }

    if (sender.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformIdentity;  // 復原形變
        }];
    }
}

五、拖動手勢

UIPanGestureRecognizer *pan =
    [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];

- (void)panAction:(UIPanGestureRecognizer *)sender {
    // 回傳相對於指定 view 的位移量
    CGPoint move = [sender translationInView:self.view];
    // 若要取得速度,可用 velocityInView:
}

六、旋轉手勢

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

- (void)rotationAction:(UIRotationGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateChanged) {
        _imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
    }

    if (sender.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:1.0 animations:^{
            _imageView.transform = CGAffineTransformIdentity;
        }];
    }
}

實務上兩個關鍵補充:第一,捏合與旋轉的 scale、rotation 都是相對手勢起點的累積值,若要在同一個 view 上連續多次縮放而不跳動,應該在每次手勢結束後把值歸零,或改用 transform 相乘而非直接覆寫。第二,當多個手勢會互相衝突(例如單擊與雙擊),可用 [singleTap requireGestureRecognizerToFail:doubleTap] 指定優先順序。

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