获取到的view不同
shouldReceiveTouch 方法中,获取到的touch.view是手指触摸到的最上层的视图,
gestureRecognizerShouldBegin 获取到的是 手势添加到的视图
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
[self.view addGestureRecognizer:self.pangesture];
// Do any additional setup after loading the view.
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
NSLog(@"哈哈哈哈哈合获取获取shouldReceiveTouch获取到的view%@", touch.view);
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
NSLog(@"gestureRecognizerShouldBegin获取到的view%@", gestureRecognizer.view);
if (gestureRecognizer == self.pangesture) {
self.panNum ++;
}
return YES;
}
打印结果
由此可见,我们如果想要获取手指触摸的视图,必须要通过shouldReceiveTouch 方法,
用法,比如我们在类似抖音评论弹窗视图中,就可以通过这个
方法获取到我们当前滑动的是弹窗中的tableView,还是弹窗本身的视图
如下代码
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (gestureRecognizer == self.panGesture) {
UIView *touchView = touch.view;
while (touchView != nil) {
if ([touchView isKindOfClass:[UIScrollView class]]) {
self.scrollView = (UIScrollView *)touchView;
self.isDragScrollView = YES;
break;
}else if (touchView == self.containerView) {
self.isDragScrollView = NO;
break;
}
touchView = (UIView *)[touchView nextResponder];
}
}
return YES;
}
执行的先后顺序
shouldReceiveTouch 方法是在gestureRecognizerShouldBegin 方法之前的