swipe手勢指一根或多跟手指在螢幕上往某個方塊快速移動,

並可利用direction設定移動方向,

numberOfTouchedRequired屬性設定多少跟手指,

一個swipe手勢只能處理一個方向,需要處理兩個或兩個以上,

得再新增swipe手勢元件,以上為用storyboard的方法,

以下範例可在螢幕上滑動手勢並透過NSLog顯示手指滑動方向,

p.s 此範例因有四個方向,所以要拖入handleSwipe四個元件並設定方向屬性

 //viewcontroller.m


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)handleSwipe:(UISwipeGestureRecognizer *)sender
{
    switch (sender.direction) {
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"right");
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"lefe");
            break;
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"up");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"down");
            break;
            
        default:
            break;
    }
}
@end

 

若要用code流派控制swipe以下為範例程式碼 

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(functionName:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(functionName:)];
    UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(functionName:)];
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(functionName:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; [swipeUp setDirection:UISwipeGestureRecognizerDirectionUp]; [swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[swipeLeft setNumberOfTouchesRequired:1]; [swipeRight setNumberOfTouchesRequired:1]; [swipeUp setNumberOfTouchesRequired:1]; [swipeDown setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:swipeLeft]; [self.view addGestureRecognizer:swipeUp]; [self.view addGestureRecognizer:swipeDown]; [self.view addGestureRecognizer:swipeRight];

 

最後紀錄多個手勢產生碰撞的處理方式  

[pan requireGestureRecognizerToFail:swipeRight]; //swipeRight發生fail,就回頭觸發pan
[pan requireGestureRecognizerToFail:swipeLeft];

 

文章標籤
創作者介紹
創作者 Will 的頭像
Will

Will的部落格

Will 發表在 痞客邦 留言(0) 人氣()