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

iOS 開發之手勢識別(UIGestureRecognizer)總結

編輯:iOS 開發綜合

在 iOS 開發中,手勢識別是很常用的一塊知識,有必要做一次系統性的小總結。基本上,蘋果把常用的觸摸事件封裝成了六種手勢識別器,用法與 Button 類似,都是「目標—動作」回呼模式。以下先介紹用 Storyboard 添加,再說明純程式碼的做法。

六種手勢識別器為:

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

其實這些手勢用 touches 事件都能自己實現,蘋果只是把常用觸摸封裝起來方便使用。

一、用 Storyboard 添加手勢

和添加一個 Button 的步驟一樣:先找到對應的手勢識別器,把它拖到要添加手勢的控件上,然後為手勢拖出一個回呼 Action,在方法中添加業務邏輯即可。

二、純程式碼添加手勢

純程式碼的方式更為靈活、便於維護。實務上建議:用 Storyboard 把架構(Controller 間關係)搭起來,小的細節則用純程式碼實作,兩者搭配能大幅提升開發效率。

1. 輕擊手勢

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

// 回呼方法
-(void)tapGesture:(id)sender {
    // 輕擊後要做的事情
}

2. 長按手勢

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
longPressGesture.minimumPressDuration = 0.5; // 長按時間
[self.view addGestureRecognizer:longPressGesture];

-(void)longPressGesture:(id)sender {
    UILongPressGestureRecognizer *longPress = sender;
    if (longPress.state == UIGestureRecognizerStateBegan) {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"長按觸發" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alter show];
    }
}

手勢的常用狀態:

  • 開始:UIGestureRecognizerStateBegan
  • 改變:UIGestureRecognizerStateChanged
  • 結束:UIGestureRecognizerStateEnded
  • 取消:UIGestureRecognizerStateCancelled
  • 失敗:UIGestureRecognizerStateFailed

3. 輕掃手勢

初始化時須指定方向(上下左右)。若要監聽多個方向,需添加多個手勢,但可共用同一個回呼方法:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; // 向右
[self.view addGestureRecognizer:swipeGesture];

-(void)swipeGesture:(id)sender {
    UISwipeGestureRecognizer *swipe = sender;
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        // 向左輕掃
    }
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        // 向右輕掃
    }
}

4. 捏合手勢

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

-(void)pinchGesture:(id)sender {
    UIPinchGestureRecognizer *gesture = sender;
    if (gesture.state == UIGestureRecognizerStateChanged) {
        // scale 記錄縮放比例
        _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformIdentity; // 復原形變
        }];
    }
}

5. 拖動手勢

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

-(void)panGesture:(id)sender {
    UIPanGestureRecognizer *panGesture = sender;
    CGPoint movePoint = [panGesture translationInView:self.view]; // 取得移動點
    // 做你想做的事
}

6. 旋轉手勢

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

-(void)rotationGesture:(id)sender {
    UIRotationGestureRecognizer *gesture = sender;
    if (gesture.state == UIGestureRecognizerStateChanged) {
        _imageView.transform = CGAffineTransformMakeRotation(gesture.rotation);
    }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:1 animations:^{
            _imageView.transform = CGAffineTransformIdentity; // 取消形變
        }];
    }
}

結語

以上內容並沒有太高深的技術,主要是把 iOS 開發中的六種手勢做一次基礎梳理。掌握這六種手勢的初始化與回呼寫法,加上對手勢狀態的正確判斷,就能應付絕大多數的互動需求。

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