0

我有两个IBActions应用于一个按钮,其中一个在touchdowndown时触发,另一个在touch up时触发,但是我也有一个轻击手势应用于在双击时触发的按钮。

我已经申请NSLog查看发生了什么,结果是当我双击时触地双击都会触发(幸运的是触摸没有触发)这是有道理的 - 但是当我加倍时如何防止触地触发轻敲?

代码示例

//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.");
}
4

1 回答 1

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;
    }
于 2013-10-16T09:59:42.760 回答