iOS手势与Button冲突

添加手势的代理

1
<UIGestureRecognizerDelegate>

在代理方法中判断手势的的 VIew 是不是 UIButton

如果是 UIButton 就不响应

1
2
3
4
5
6
7
8
9
10
11
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[UIButton class]])
{
return NO;
}
else
{
return YES;
}
}

或者判断点击的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:self.view];

if(CGRectContainsPoint(self.btn.frame, location))
{
return NO;
}
else
{
return YES;
}
}