在做UIButton事件攔截的時候,我知道button點擊後會執行sendAction:to:forEvent:方法,所以我可以hook這個方法做些其他事,那UIView的Tap事件,該攔截哪個方法呢?
sendAction:to:forEvent:
如下程式碼所新增的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; }
如下程式碼所新增的tap 事件
可以用shouldReceiveTouch 攔截