当UITapGestureRecognizer在双击时触发时,防止触发IBAction事件被触发

时间:2013-10-16 07:08:11

标签: iphone ios xcode gesture

我有两个IBActions应用于按钮,其中一个触发触地向下,另一个在触摸 ,但是我也有一个点击手势应用于双击上的按钮。

我已经应用了NSLog来查看发生了什么,结果是当我双击时,触地双击都会触发(幸运的是< em> touch up 不会触发)这是有意义的 - 但是当我双击时,如何阻止触碰?

代码示例

//fired on touch down 
-(IBAction)touchdown:(id)sender
{
    NSLog(@"touching down.");
}

//fired on touch up
-(IBAction)touchup:(id)sender
{   
    NSLog(@"taking off.");
}

//fired on double click
-(IBAction)boost:(UITapGestureRecognizer *)sender
{
    NSLog(@"goodbye.");
}

1 个答案:

答案 0 :(得分:0)

导入代理<UIGestureRecognizerDelegate>

我认为你是在viewDidLoad方法中完成的,

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(boost:)];
tapGestureRecognizer.delegate = self;
tapGestureRecognizer.numberOfTapsRequired = 2;
[self.button addGestureRecognizer:tapGestureRecognizer];

您需要根据您的降落坐标

来实现此委托方法
  -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        CGPoint coords = [touch locationInView:self.button];
        NSLog(@"Coords: %g, %g", coords.x, coords.y);
        if (coords.y < 21 && coords.y > 25)
           return NO;
        else
           return YES;
    }