手势冲突
手势冲突主要的三种解决思路:
①设置手势优先级
requireGestureRecognizerToFail:
②阻止手势向事件传递链执行cancelsTouchesInView
、delaysTouchesBegan
③实现协议方法UIGestureRecognizerDelegate
1、自定义手势优先级
自定义多手势冲突。例如我们设置的单次点击、双击和多指点击手势,需要设置优先识别双击手势,识别失败后再识别单击手势;
//单指单击
UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
singleFingerOne.numberOfTouchesRequired = 1; //手指数
singleFingerOne.numberOfTapsRequired = 1; //tap次数
[self.view addGestureRecognizer:singleFingerOne];
//单指双击
UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
singleFingerTwo.numberOfTouchesRequired = 1;
singleFingerTwo.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:singleFingerTwo];
//双指单击
UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleFingerEvent:)];
doubleFingerOne.numberOfTouchesRequired = 2;
doubleFingerOne.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:doubleFingerOne];
//双指双击
UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleFingerEvent:)];
doubleFingerTwo.numberOfTouchesRequired = 2;
doubleFingerTwo.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleFingerTwo];
// 优先级设置
//如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
//同理双指亦是如此
[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
//处理单指事件
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender{
if (sender.numberOfTapsRequired == 1) {
//单指单击
NSLog(@"单指单击");
}else if(sender.numberOfTapsRequired == 2){
//单指双击
NSLog(@"单指双击");
}
}
//处理双指事件
- (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender{
if (sender.numberOfTapsRequired == 1) {
//双指单击
NSLog(@"双指单击");
}else if(sender.numberOfTapsRequired == 2){
//双指双击
NSLog(@"双指双击");
}
}
2、阻止手势向响应链传递执行
当自定义手势和系统手势冲突,可以使用手势的两个属性:cancelsTouchesInView
、delaysTouchesBegan
进行处理。
cancelsTouchesInView
(是否取消向事件响应链传递):
默认 YES,自定义的手势响应后,系统手势不再响应,但自定义手势识别前,会先执行系统手势。 设置为 NO 后,自定义手势和系统手势会同时识别响应。delaysTouchesBegan
(延迟响应链的识别):
默认 NO,先执行响应链中的方法(系统方法),识别到自定义手势后,不再执行系统方法。 设置为 YES 后,优先识别自定义手势,当自定义手势识别失败后才会响应系统方法
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
testView.backgroundColor = [UIColor yellowColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
// 阻止手势向事件传递链执行 默认是 YES
tap.cancelsTouchesInView = NO;
[testView addGestureRecognizer:tap];
[self.view addSubview:testView];
UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
testBtn.frame = CGRectMake(0, 0, 100, 100);
testBtn.backgroundColor = [UIColor cyanColor];
[testBtn addTarget:self action:@selector(testBtnClick) forControlEvents:UIControlEventTouchUpInside];
[testView addSubview:testBtn];
- (void)tap:(UITapGestureRecognizer *)sender{
NSLog(@"点击view");
}
-(void)testBtnClick{
NSLog(@"点击button");
}
3、实现协议方法 UIGestureRecognizerDelegate 控制手势冲突
//手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
//开始进行手势识别时调用的方法,返回NO则结束,不再触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
//是否支持多时候触发,返回YES,则可以多个手势一起触发方法,返回NO则为互斥
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
//下面这个两个方法也是用来控制手势的互斥执行的
//这个方法返回YES,第一个手势和第二个互斥时,第一个会失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
//这个方法返回YES,第一个和第二个互斥时,第二个会失效
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
常用的是第二个协议方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer {
UIPanGestureRecognizer *ges = (UIPanGestureRecognizer *)otherGestureRecognizer;
if ([ges isKindOfClass:NSClassFromString(@"BMKTapDetectingView")]) {
return NO;
}
if([ges isKindOfClass:[UICollectionViewCell class]]){
return NO;
}
// 手势是 UIScrollViewPan 且 可以左右滑动, 已经滑到最左端
if ([ges isKindOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")]) {
return [ges.view isKindOfClass:[UIScrollView class]]
&& (((UIScrollView *)ges.view).contentSize.width > ges.view.bounds.size.width * 1.5)
&& (((UIScrollView *)ges.view).contentOffset.x == 0);
}
// 手势不是 UIPanGestureRecognizer
if (![ges isKindOfClass:[UIPanGestureRecognizer class]]) {
return NO;
}
// 手势落点在屏幕左边1/3
if ([ges locationInView:nil].x <= [UIScreen mainScreen].bounds.size.width / 3) {
return NO;
}
// 手势是 上下滑动
CGPoint offset = [ges translationInView:nil];
if (fabs(offset.x) <= fabs(offset.y)) {
return NO;
}
// 默认
return YES;
}