1. 程式人生 > >iPhone開發筆記──手勢操作:…

iPhone開發筆記──手勢操作:…

1.手指點選事件 [html]   //單指單擊   UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self                                                                                     action:@selector(handleSingleFingerEvent:)];   singleFingerOne.numberOfTouchesRequired = 1; //手指數   singleFingerOne.numberOfTapsRequired = 1; //tap次數   singleFingerOne.delegate = self;   //單指雙擊   UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self                                                                                     action:@selector(handleSingleFingerEvent:)];   singleFingerTwo.numberOfTouchesRequired = 1;   singleFingerTwo.numberOfTapsRequired = 2;   singleFingerTwo.delegate = self;   //雙指單擊   UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self                                                                                     action:@selector(handleDoubleFingerEvent:)];   doubleFingerOne.numberOfTouchesRequired = 2;   doubleFingerOne.numberOfTapsRequired = 1;   doubleFingerOne.delegate = self;   UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self                                                                                     action:@selector(handleDoubleFingerEvent:)];   doubleFingerTwo.numberOfTouchesRequired = 2;   doubleFingerTwo.numberOfTapsRequired = 2;   doubleFingerTwo.delegate = self;   //如果不加下面的話,當單指雙擊時,會先呼叫單指單擊中的處理,再呼叫單指雙擊中的處理   [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];   //同理雙指亦是如此   [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];   [self.view addGestureRecognizer:singleFingerOne];   [self.view addGestureRecognizer:singleFingerTwo];   [self.view addGestureRecognizer:doubleFingerOne];   [self.view addGestureRecognizer:doubleFingerTwo];   [singleFingerOne release];   [singleFingerTwo release];   [doubleFingerOne release];   [doubleFingerTwo release];   //處理單指事件   - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender   {       if (sender.numberOfTapsRequired == 1) {           //單指單擊           NSLog(@"單指單擊");       }else if(sender.numberOfTapsRequired == 2){           //單指雙擊           NSLog(@"單指雙擊");       }   }   //處理雙指事件   - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender   {       if (sender.numberOfTapsRequired == 1) {           //雙指單擊           NSLog(@"雙指單擊");       }else if(sender.numberOfTapsRequired == 2){           //雙指雙擊           NSLog(@"雙指雙擊");       }   }   2.手指滑動事件 [html]  UISwipeGestureRecognizer *recognizer;       recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];       [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];       [[self view] addGestureRecognizer:recognizer];       [recognizer release];       recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];       [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];       [[self view] addGestureRecognizer:recognizer];       [recognizer release];       recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];       [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];       [[self view] addGestureRecognizer:recognizer];       [recognizer release];       recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];       [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];       [[self view] addGestureRecognizer:recognizer];       [recognizer release];   -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {     if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft||recognizer.direction==UISwipeGestureRecognizerDirectionDown) {         NSLog(@"swipe left");         //執行程式         [self ReadNextPage];     }     if(recognizer.direction==UISwipeGestureRecognizerDirectionRight||recognizer.direction == UISwipeGestureRecognizerDirectionUp) {         NSLog(@"swipe right");         //執行程式         [self ReadLastPage];     } }