UITapGestureRecognizer 区分多指 单双击

LINK ADDRESS:http://www.cocoachina.com/bbs/read.php?tid=73117&fpage=3

前几天又有山大的MM问我,怎么区分单双击。
我说到坛子里找,没过多久,她又说,用touches方法控制的单双击不太好用,还有什么事件冲突……
没办法,帮忙写了一个,也发上来,知道的可掠过,需要的请自取。

 
复制代码
  1. - (void)viewDidLoad {
  2.     
  3.     UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
  4.     singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
  5.     
  6.     UITapGestureRecognizer *singleTapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
  7.     singleTapTwo.numberOfTouchesRequired = 2; singleTapTwo.numberOfTapsRequired = 1; singleTapTwo.delegate = self;
  8.     
  9.     UITapGestureRecognizer *doubleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  10.     doubleTapOne.numberOfTouchesRequired = 1; doubleTapOne.numberOfTapsRequired = 2; doubleTapOne.delegate = self;
  11.     
  12.     UITapGestureRecognizer *doubleTapTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  13.     doubleTapTwo.numberOfTouchesRequired = 2; doubleTapTwo.numberOfTapsRequired = 2; doubleTapTwo.delegate = self;
  14.     
  15.     [singleTapOne requireGestureRecognizerToFail:doubleTapOne]; // Single tap requires double tap to fail
  16.     [singleTapTwo requireGestureRecognizerToFail:doubleTapTwo];
  17.     
  18.     [self.view addGestureRecognizer:singleTapOne]; [singleTapOne release];
  19.     [self.view addGestureRecognizer:singleTapTwo]; [singleTapTwo release];
  20.     [self.view addGestureRecognizer:doubleTapOne]; [doubleTapOne release];
  21.     [self.view addGestureRecognizer:doubleTapTwo]; [doubleTapTwo release];
  22.     
  23.     
  24.     [super viewDidLoad];
  25. }
  26. - (void)handleSingleTap:(UITapGestureRecognizer *)sender {
  27.     if (sender.numberOfTouchesRequired == 1) {
  28.         NSLog(@"Single Tap with a finger.");
  29.     }
  30.     else if (sender.numberOfTouchesRequired == 2) {
  31.         NSLog(@"Single Tap with two finger.");
  32.     }
  33. }
  34. - (void)handleDoubleTap:(UITapGestureRecognizer *)sender {
  35.     if (sender.numberOfTouchesRequired == 1) {
  36.         NSLog(@"Double Tap with a finger.");
  37.     }
  38.     else if (sender.numberOfTouchesRequired == 2) {
  39.         NSLog(@"Double Tap with two finger.");
  40.     }
  41. }

你可能感兴趣的:(gesture)