objective-c - 想问对于UIView想做Tap点击事件监控,拦截哪个方法呢?

【字号: 日期:2023-11-30浏览:41作者:雯心

问题描述

在做UIButton事件拦截的时候,我知道button点击后会执行sendAction:to:forEvent:方法,所以我可以hook这个方法做些其他事,那UIView的Tap事件,该拦截哪个方法呢?

问题解答

回答1:

如下代码添加的tap 事件

self.backgroundTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackgroundTapGesture:)];self.backgroundTapRecognizer.delegate = self;[self.maskView addGestureRecognizer:self.backgroundTapRecognizer];

可以用shouldReceiveTouch 拦截

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ if ([touch.view isDescendantOfView:self.popupView]) { //判断条件,比如是popView //NSLog(@'NO');return NO; //点击无效 } return YES;}

相关文章: