手势处理

在ios开发中,需用到对于手指的不同操作,以手指点击为例:分为单指单击、单指多击、多指单击、多指多击。对于这些事件进行不同的操作处理,由于使用系统自带的方法通过判断touches不太容易处理,而且会有事件之间的冲突。

 

接下来,通过以UITapGestureRecognizer 手势实现此功能需求 代码如下

ViewController.m中的viewDidLoad方法:

   1:  //单指单击
   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属性。

对于其他手势例如UISwipeGestureRecognizerUILongPressGestureRecognizer,UILongPressGestureRecognizer的操作使用类似处理。

posted @ 2012-08-31 18:26  ValeTu  阅读(268)  评论(0编辑  收藏  举报