某个界面点击空白处让键盘回收,我写了一个手势方法
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
singleTap.delegate = self;
[self.view addGestureRecognizer:singleTap];
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer
{
[self.view endEditing:YES];
}
这样就会收起键盘
问题: 当点击cell的时候发现没反应, 点击事件和手势发生了冲突
解决: 实现手势的代理方法, 判断点击 的哪个类, 做出相应的逻辑业务
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// 输出点击的view的类名
NSLog(@" 输出点击的view的类名%@", NSStringFromClass([touch.view class]));
// 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"] || [NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCell"]) {
return NO;
}
return YES;
}
贵在点滴积累!!!
网友评论