正常我们使用endingEditWhenDraging来收起键盘,当然,如果再加上tap手势.用户体验会更好一点.
使用tap手势的话,代码很简单,在tapEvent方法里面收起键盘
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEvent:)];
tapGesture.numberOfTapsRequired = 1;
[self.tableView addGestureRecognizer:tapGesture];
但是这样你会发现,tableView的didSelectRowAtIndexPath不响应了.点击每行cell,touch的view是cell的contentView,所以我们可以通过判断touch的view是否为cell的contentView,是就不响应tap的方法,这样就不会和didSelectRowAtIndexPath方法冲突.
具体可通过重写UIGestureRecognizerDelegate的- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch方法,代码如下,有问题留言
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEvent:)];
tapGesture.delegate = self;
tapGesture.numberOfTapsRequired = 1;
[self.tableView addGestureRecognizer:tapGesture];
/** 实现手势的代理方法 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// 若为UITableViewCellContentView,就不接受tap事件
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
return NO;
}
return YES;
}
网友评论