单击TapGestureRecognizer

时间:2014-04-14 18:19:45

标签: ios uitapgesturerecognizer

我正在尝试将此代码用于轻敲手势识别器,当需要的数量设置为2时它可以正常工作,但是当我将所需的抽头数设置为1时它会停止运行。感谢您帮助我们开展工作。

   UITapGestureRecognizer *doubleTap =
    [[UITapGestureRecognizer alloc]
     initWithTarget:self
     action:@selector(tapDetected:)];
   [doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
    [self.view addGestureRecognizer:doubleTap];

3 个答案:

答案 0 :(得分:0)

你试试这个: -

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

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

[singleTap requireGestureRecognizerToFail:doubleTap];

这适合我。

答案 1 :(得分:0)

如果您只希望接收器响应一次/第一次触摸,则setDelaysTouchesBegan:NO由于这用于处理UITouchPhaseBegan中的触摸,因此会对其进行分析并阻止其传送。文档中的财产讨论:

  

当此属性的值为NO(默认值)时,视图将与接收器并行分析UITouchPhaseBegan和UITouchPhaseMoved中的触摸事件。当属性的值为YES时,窗口会暂停UITouchPhaseBegan阶段中触摸对象到视图的传递。如果手势识别器随后识别其手势,则丢弃这些触摸对象。但是,如果手势识别器无法识别其手势,则窗口会以touchesBegan:withEvent:消息(以及可能的后续touchesMoved:withEvent:消息将这些对象传递给视图,以通知其触摸的当前位置) 。将此属性设置为YES可防止视图处理UITouchPhaseBegan阶段中可能被识别为此手势一部分的任何触摸。

答案 2 :(得分:0)

在我看来,你只是没有设置你的GestureRecognizer的代表。你确定双击是否成功射击?当我将代码放入我的项目时,我看到相同的行为,但正确设置委托并使用shouldRecognizeSimultaneouslyWithGestureRecognizer使它能够正确识别单击。

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                            action:@selector(tapDetected:)];
[doubleTap setDelegate:self];
[doubleTap setNumberOfTapsRequired : 1];
[doubleTap setDelaysTouchesBegan : YES];
[picker addGestureRecognizer:doubleTap];

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:UITapGestureRecognizer.class] &&
        [otherGestureRecognizer isKindOfClass:UITapGestureRecognizer.class])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}