14

例如,我有一个需要两种不同手势的视图:

点击执行操作 A. 双击执行操作 B.

问题在于 UITapGestureRecognizer 我只能设置所需的最少点击次数。在双击手势识别器识别双击之前,单击手势识别器识别点击。

例子:

_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognized:)];
_tapGestureRecognizer.numberOfTouchesRequired = 1;
_tapGestureRecognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:_tapGestureRecognizer];

_doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGestureRecognized:)];
_doubleTapGestureRecognizer.numberOfTouchesRequired = 1;
_doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[self addGestureRecognizer:_doubleTapGestureRecognizer];

即使我非常快速地双击它,它也总是能识别单击。如何设置它以便轻按手势识别器等待并查看双击手势识别器是否识别?

4

4 回答 4

49

这是我在我的一个旧项目中使用的,我希望它对你有所帮助。

UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:    self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1; 
[self.view addGestureRecognizer:singleTap];

 UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:   self action:@selector(doDoubleTap)] autorelease];
 doubleTap.numberOfTapsRequired = 2; 
 [self.view addGestureRecognizer:doubleTap];

  [singleTap requireGestureRecognizerToFail:doubleTap];
于 2013-10-01T23:13:53.143 回答
7

在 swift 3 和 swift 4 上:

let singleTap = UITapGestureRecognizer(target: self, action: #selector(doSingleTap))
singleTap.numberOfTapsRequired = 1

self.view.addGestureRecognizer(singleTap)

let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doDoubleTap))
doubleTap.numberOfTapsRequired = 2

self.view.addGestureRecognizer(doubleTap)

singleTap.require(toFail: doubleTap)
于 2016-11-02T04:41:23.103 回答
0
   {  UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTapGesture:)];
        [doubleTap setDelegate:self];
        doubleTap.numberOfTapsRequired = 2;
        [self.headerView addGestureRecognizer:doubleTap];

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapGesture:)];
        singleTap.numberOfTapsRequired = 1;
        [singleTap setDelegate:self];
        [doubleTap setDelaysTouchesBegan:YES];
        [singleTap setDelaysTouchesBegan:YES];
        [self.headerView addGestureRecognizer:singleTap];
  }  
        - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
            return  YES;
        }

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        return YES;
    }

在您想要的任何方法中创建手势识别器。例如,如果您想在 tableView 中为 headerView 的点击手势在里面创建这些点击手势

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

你的班级应该实现 UIGestureRecognizerDelegate。上面提到的是该委托类的 2 个方法。

于 2017-05-10T13:29:28.333 回答
-1

我使用 XCode Monkey 的回答找到了单击延迟的解决方案。使用这种方法,您可以以毫秒为单位配置单击和双击之间的阈值。该技术使用简单的 UIButton 和 NSTimer 来区分单击和双击。

@property NSTimer       *tapTimer;
@property BOOL           isDoubleTap;

  UIButton *yourButton = [UIButton new];
  yourButton.frame = // set your frame
  [yourButton addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
  [self.view yourButton];


-(void)handleTap:(UIButton*)button
{
  [self.tapTimer invalidate];

  if (self.isDoubleTap)
  {
    [self doDoubleTap];
    return;
  }
  self.isDoubleTap = YES;
  // CHANGE THE 0.3 SECOND THRESHOLD BETWEEN SINGLE/DOUBLE TAP TO YOUR HEART'S CONTENT
  self.tapTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(doSingleTap) userInfo:nil repeats:NO];
}

-(void)doSingleTap
{
  self.isDoubleTap = NO;
}

-(void)doDoubleTap
{
  self.isDoubleTap = NO;
}
于 2015-06-15T18:42:32.570 回答