你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS學習筆記18—UITapGestureRecognizer 的用法

iOS學習筆記18—UITapGestureRecognizer 的用法

編輯:IOS開發綜合


/單指單擊
   2:      UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
   3:                                      action:@selector(handleSingleFingerEvent:)];
   4:      singleFingerOne.numberOfTouchesRequired = 1; //手指數
   5:      singleFingerOne.numberOfTapsRequired = 1; //tap次數
   6:      singleFingerOne.delegate = self;
   7:     
   8:      //單指雙擊
   9:      UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
  10:                                       action:@selector(handleSingleFingerEvent:)];
  11:      singleFingerTwo.numberOfTouchesRequired = 1;
  12:      singleFingerTwo.numberOfTapsRequired = 2;
  13:      singleFingerTwo.delegate = self;
  14:     
  15:      //雙指單擊
  16:      UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
  17:                                       action:@selector(handleDoubleFingerEvent:)];
  18:      doubleFingerOne.numberOfTouchesRequired = 2;
  19:      doubleFingerOne.numberOfTapsRequired = 1;
  20:      doubleFingerOne.delegate = self;
  21:     
  22:      UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
  23:     action:@selector(handleDoubleFingerEvent:)];
  24:      doubleFingerTwo.numberOfTouchesRequired = 2;
  25:      doubleFingerTwo.numberOfTapsRequired = 2;
  26:      doubleFingerTwo.delegate = self;
  27:     
  28:      //如果不加下面的話,當單指雙擊時,會先調用單指單擊中的處理,再調用單指雙擊中的處理
  29:      [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
  30:      //同理雙指亦是如此
  31:      [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
  32:     
  33:      [self.view addGestureRecognizer:singleFingerOne];
  34:      [self.view addGestureRecognizer:singleFingerTwo];
  35:      [self.view addGestureRecognizer:doubleFingerOne];
  36:      [self.view addGestureRecognizer:doubleFingerTwo];
  37:     
  38:      [singleFingerOne release];
  39:      [singleFingerTwo release];
  40:      [doubleFingerOne release];
  41:      [doubleFingerTwo release];
處理事件的方法,代碼:

   1:  //處理單指事件
   2:  - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
   3:  {
   4:      if (sender.numberOfTapsRequired == 1) {
   5:          //單指單擊
   6:          NSLog(@"單指單擊");
   7:      }else if(sender.numberOfTapsRequired == 2){
   8:          //單指雙擊
   9:          NSLog(@"單指雙擊");
  10:      }
  11:  }
  12:  //處理雙指事件
  13:  - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
  14:  {
  15:      if (sender.numberOfTapsRequired == 1) {
  16:          //雙指單擊
  17:          NSLog(@"雙指單擊");
  18:      }else if(sender.numberOfTapsRequired == 2){
  19:          //雙指雙擊
  20:          NSLog(@"雙指雙擊");
  21:      }
  22:  }
將相應代碼復制到你的工程中即可使用,由於代碼中已經有詳細的解釋說明,這裡就不在重復解釋了。

代碼中只是列舉了單指與雙指對於單擊或多擊的處理,同理多指的操作需修改numberOfTouchesRequired屬性,對點擊的次數需修改numberOfTapsRequired屬性。

對於其他手勢例如UISwipeGestureRecognizer,UILongPressGestureRecognizer,UILongPressGestureRecognizer的操作使用類似處理。

 


 

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