以UITableView的cell点击事件为例
1、初始化
UITapGestureRecognizer *tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_tapGester)];
[self addGestureRecognizer:tapGester];
///首先让UITapGestureRecognizer不要吃掉子窗口的事件:
tapGester.cancelsTouchesInView = NO;
///然后设置自身的代理:
tapGester.delegate = self;
2、视图控制器遵守代理
@interface ZJZDTableMenuView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
@end
3、实现代理方法
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
return YES;
}