UITableView 不会与单个手势识别器接触 [英] UITableView doesn't get touches with single gesture recognizer

查看:23
本文介绍了UITableView 不会与单个手势识别器接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UITextField、UIButton 和 UITable 视图的 UIView.文本字段和按钮会破坏搜索栏,然后将结果加载到表格视图中.

I have a UIView with a UITextField, UIButton and UITable view. The textfield and button compromise a search bar and the results are then loaded into the table view.

我想这样做,以便他们关闭键盘.如果用户在编辑文本字段时点击某些内容.我的策略是向 UIView 添加手势识别器,但是手势识别器似乎会拦截来自表视图的所有触摸,而您 |tableView:didSelectCellAtIndexPath:|永远不会被调用.有趣的是(至少对我而言)是当用户编辑字段时 UIButton 仍然可以点击,即使 UITableView 不是.

I'd like to make it so they keyboard dismisses. If the user taps something when they are editing the text field. My strategy would be to add a gesture recognizer to the UIView, but then gesture recognizer seems to intercept all the touches from the table view and you |tableView:didSelectCellAtIndexPath:| never gets called. Whats interesting (to me at least) is that the UIButton is still tap-able when the user is editing the field even though the UITableView isn't.

我试过实现 |gestureRecognizer::shouldRecognizeSimultaneouslyWithGestureRecognizer:|总是返回是,但这无济于事.我也试过设置

I've tried implementing |gestureRecognizer::shouldRecognizeSimultaneouslyWithGestureRecognizer:| to alway return yes, but that doesn't help. I've also tried setting

singleTapRecognizer.cancelsTouchesInView = NO;

这也无济于事.

我很高兴在文本字段调用 |textFieldDidBeginEditing:| 时添加和删除手势识别器的想法.和 |textFieldDidFinishEditing:|,虽然这感觉很乱,但在编辑文本字段时仍然需要点击两次才能触摸单元格(一次关闭键盘并移除识别器,另一次点击单元格).

I'm happy with the idea of adding and removing the gesture recognizer when the text field calls |textFieldDidBeginEditing:| and |textFieldDidFinishEditing:|, though this feels messy and it still takes two taps to touch a cell when you're editing the text field (one to dismiss they keyboard and remove the recognizer, and one to tap the cell).

有没有更好的方法?

相关代码如下:

- (void)loadView {
  [super loadView];

  self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  self.scrollView.backgroundColor = [FDEColors viewBackgroundColor];
  self.view = self.scrollView;

  self.searchField = [[UITextField alloc] initWithFrame:CGRectZero];
  self.searchField.placeholder = @"What are you looking for?";
  self.searchField.backgroundColor = [FDEColors textFieldBackgroundColor];
  self.searchField.clipsToBounds = YES;
  self.searchField.layer.borderColor = [[FDEColors buttonColor] CGColor];
  self.searchField.layer.borderWidth = 1.f;
  self.searchField.returnKeyType = UIReturnKeySearch;
  self.searchField.delegate = self;
  [self.view addSubview:self.searchField];

  self.searchButton = [[UIButton alloc] initWithFrame:CGRectZero];
  self.searchButton.backgroundColor = [FDEColors buttonColor];
  [self.searchButton setTitle:@"Search" forState:UIControlStateNormal];
  [self.searchButton addTarget:self
                        action:@selector(searchPressed:)
              forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:self.searchButton];

  self.resultsTableView =
      [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  self.resultsTableView.delegate = self;
  self.resultsTableView.dataSource = self;
  self.resultsTableView.backgroundColor = [FDEColors viewBackgroundColor];
  [self.resultsTableView setSeparatorInset:UIEdgeInsetsZero];
  self.resultsTableView.layoutMargins = UIEdgeInsetsZero;
  [self.resultsTableView registerClass:[FDESearchResultsCell class]
                forCellReuseIdentifier:[FDESearchResultsCell reuseIdentifier]];
  [self.view addSubview:self.resultsTableView];

  self.singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                     action:@selector(dismissKeyboard)];
  [self.view addGestureRecognizer:self.singleTapRecognizer];
}

- (void)dismissKeyboard {
  [[self view] endEditing:YES];
}

推荐答案

尝试实现手势识别器的委托方法:

Try implementing the gesture recognizer's delegate method:

- (BOOL) gestureRecognizer:ShouldReceiveRouch:

在此方法中,检查触摸的位置.如果它在 tableview 内,则返回 no,以便 tableview 可以接收触摸.否则,返回 YES 并让识别器处理触摸.

In this method, check for the touch's location. If it's inside the tableview, return no, so the tableview can receive the touch. Otherwise, return YES and let the recognizer handle the touch.

至于在识别器存在的情况下接收触摸的按钮,从 iOS6 开始,Apple 决定在识别手势时给予按钮和其他一些控件优先级.不过,它仅适用于对抗手势,在您的情况下只需轻按一下.例如,如果您还有一个平移识别器,则识别器将具有优先权,而不是按钮.

As for the button receiving the touch despite the recognizer's existence, as of iOS6 Apple decided to give buttons and some other controls priority when it comes to recognizing gestures. It only applies to antagonizing gestures though, in your case a single tap. If for example you also had a pan recognizer, the recognizer would have precedence, not the button.

编辑 2:

上述方法的一个示例实现:

An example implementation of the method mentioned above:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// Determine if the touch is inside the custom subview
if ([touch view] == self.yourTableView){
    // If it is, prevent all of the delegate's gesture recognizers
    // from receiving the touch
    return NO;
}
return YES;

}

这篇关于UITableView 不会与单个手势识别器接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆