【问题标题】:UITableView doesn't get touches with single gesture recognizerUITableView 无法与单手势识别器接触
【发布时间】:2015-04-04 17:35:09
【问题描述】:

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

我想让他们键盘消失。如果用户在编辑文本字段时点击了某些东西。我的策略是在 UIView 中添加一个手势识别器,但是手势识别器似乎会拦截来自表格视图的所有触摸,而您 |tableView:didSelectCellAtIndexPath:|永远不会被调用。有趣的是(至少对我而言)是 UIButton 在用户编辑字段时仍然可以点击,即使 UITableView 不是。

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

singleTapRecognizer.cancelsTouchesInView = NO;

这也无济于事。

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

有没有更好的办法?

相关代码如下:

- (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];
}

【问题讨论】:

    标签: ios objective-c uitableview uiscrollview


    【解决方案1】:

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

    - (BOOL) gestureRecognizer:ShouldReceiveRouch:
    

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

    编辑:至于尽管存在识别器但仍接收触摸的按钮,从 iOS6 开始,Apple 决定在识别手势时优先考虑按钮和其他一些控件。不过,它仅适用于对抗手势,在您的情况下,只需轻按一下即可。例如,如果您还有一个平移识别器,则识别器将具有优先权,而不是按钮。

    编辑 2:

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

    -(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;
    

    }

    【讨论】:

    • 整洁。除了 touch.view 实际上在 UITableViewCellContentView 上之外,这工作得相对较好。方便的是,您无法测试 UITableViewCellContentView。我从 link 偷了答案,但这个函数应该这样做:-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([touch.view isDescendantOfView:self.resultsTableView] || touch.view == self.resultsTableView) { return NO; } return YES; }
    • 是的,必须像这样挖掘并不是最优雅的解决方案,但它确实有效。永远记住,如果他们决定更改 UITableview 的实现,它可能会中断。最近在类似的情况下,我使用了上面的函数,但是将 tableview 的 origin.y 与 touch 进​​行了比较,而不是我们上面使用的。使用更适合您的情况。
    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2015-07-23
    • 1970-01-01
    相关资源
    最近更新 更多