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

使用 UISwipeGestureRecognizer 處理四向滑動

編輯:iOS 相關資訊

iOS 提供 UISwipeGestureRecognizer,可快速辨識上、下、左、右滑動。建立時只要指定方向與 action,再加入 view 即可。

UISwipeGestureRecognizer *up = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
up.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:up];

若要同時支援四個方向,可分別建立四個 recognizer,並在 handleSwipeFrom: 中依 direction 判斷:

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    switch (recognizer.direction) {
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"swipe up");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"swipe down");
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"swipe left");
            break;
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"swipe right");
            break;
        default:
            break;
    }
}

在 ARC 專案中不需要手動 release;若為舊式 MRC,記得在加入 view 後釋放 temporary recognizer。

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