如何过滤单击事件和双击事件的快速多次点击

时间:2015-03-04 09:48:29

标签: ios uigesturerecognizer

我有以下手势设置:

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

UITapGestureRecognizer *doubleClick = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(doubleClickDetected:)];
doubleClick.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleClick];

[singleTap requireGestureRecognizerToFail:doubleClick];

当我快速点击3次时,我发现它会被翻译成一个双击事件和一个单击事件,并为我的应用程序引入一个错误。

如果用户点击3次或更多次,我想要的是,只会触发双击事件。有人可以帮忙吗?提前谢谢。

3 个答案:

答案 0 :(得分:2)

为了iOS 10和Swift 3开发人员的利益,我正在回答这个问题。

目前在iOS 10中使用Swift 3这个问题不存在。如果不处理三重点击被降级为双击并且相应的操作被触发。请尝试以下示例:

Swift 3解决方案:

doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))
doubleTap.numberOfTapsRequired = 2


singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))
singleTap.numberOfTapsRequired = 1

singleTap.require(toFail: doubleTap)

self.view.addGestureRecognizer(doubletap)
self.view.addGestureRecognizer(singleTap)

答案 1 :(得分:1)

我创建了一个像下面这样的自定义识别器也解决了我的问题:

@implementation MyTapGestureRecognizer

/**
 *  touchesBegan for custom taps will filter > 2 taps
 *
 *  @param touches touches the recognizer gets
 *  @param event   related event
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    if ([touches count] != 1) {
        return;
    } else {
        UITouch *touch = [touches anyObject];
        if (touch.tapCount >= 3) {
            self.state = UIGestureRecognizerStateFailed;
            return;
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
}

- (void)reset {
    [super reset];
}

@end

答案 2 :(得分:0)

您可以使用手势识别器的委托在双击后暂时禁用这两种手势。双击后,下面的代码会禁用手势0.6秒。

您需要一些属性

@property int disableGestures;
@property CFTimeInterval timeStamp;

和一些额外的初始化

self.disableGestures = NO;
singleTap.delegate = self;
doubleClick.delegate = self;

并且您需要符合<UIGestureRecognizerDelegate>协议,并实施shouldReceiveTouch方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ( self.disableGestures )
    {
        if ( CACurrentMediaTime() - self.timeStamp < 0.6 )
            return NO;
        self.disableGestures = NO;
    }
    return YES;
}

剩下的就是双击后禁用手势

- (void)doubleClickDetected:(UITapGestureRecognizer *)gesture
{
    self.disableGestures = YES;
    self.timeStamp = CACurrentMediaTime();

    // normal processing for the double click goes here
}