如何为单击事件和双击事件过滤快速多次点击
How to filter a quick multiple tap for single Tap event and double tap event
我有以下手势设置:
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 次或更多次,只会触发双击事件。有人可以帮忙吗?提前致谢。
您可以使用手势识别器的委托在双击后暂时禁用这两个手势。下面的代码在双击后禁用手势 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
}
我创建了如下所示的自定义识别器也解决了我的问题:
@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
我回答这个问题是为了 iOS 10 位和 Swift 3 位开发人员。
目前在iOS10和Swift3中不存在这个问题。如果未处理,三次点击将降级为双击,并触发相应的操作。试试下面的例子:
Swift3 解:
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)
我有以下手势设置:
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 次或更多次,只会触发双击事件。有人可以帮忙吗?提前致谢。
您可以使用手势识别器的委托在双击后暂时禁用这两个手势。下面的代码在双击后禁用手势 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
}
我创建了如下所示的自定义识别器也解决了我的问题:
@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
我回答这个问题是为了 iOS 10 位和 Swift 3 位开发人员。
目前在iOS10和Swift3中不存在这个问题。如果未处理,三次点击将降级为双击,并触发相应的操作。试试下面的例子:
Swift3 解:
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)