您是否为滚动视图添加了点击手势,如果没有,请尝试添加它?
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignsKeyboard)];
[self.scrollView addGestureRecognizer:singleTap];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
在你的界面文件中添加委托 UIGestureRecognizerDelegate 然后实现点击手势的 delgate 方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (self.scrollView.superview != nil)
{
if ([touch.view isKindOfClass:[UIButton class]]||[touch.view.superview.superview isKindOfClass:[UITableViewCell class]]||[touch.view.superview.superview.superview isKindOfClass:[UITableViewCell class]]||[touch.view.superview isKindOfClass:[UITableViewCell class]])
// we touched our control surface
{
return NO; // ignore the touch
}
}
return YES; // handle the touch
}
//要在滚动触摸上执行其他操作,例如退出键盘,如果需要,请使用
-(void) resignsKeyboard
{
[self.view endEditing:YES];
[self.scrollView setContentOffset:CGPointMake(0,0) animated:YES];
}