1. 注册手势
/** * 注册手势 */ -(void)gestureReg{ //放弃第一响应者 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; [self.view addGestureRecognizer:tap]; }
2. 收起键盘
- (void)tapAction { [_m_textSearchView resignFirstResponder]; }
3. 排除手势
如果添加了手势,其它元素的响应事件也收到影响,其它元素就获取不到点击事件了。那该怎么做呢?需要排除要进行响应的元素。另外,按钮的点击事件不受整个影响。
3.1. 添加UIGestureRecognizerDelegate
3.2. tap.delegate=self;
3.3. 重写delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { // 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件 if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) { return NO; } return YES; }