gpt4 book ai didi

ios - UITableView 不接触单个手势识别器

转载 作者:可可西里 更新时间:2023-11-01 03:35:01 25 4
gpt4 key购买 nike

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

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

我试过执行 |gestureRecognizer::shouldRecognizeSimultaneouslyWithGestureRecognizer:|总是返回 yes,但这无济于事。我也试过设置

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

最佳答案

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

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

关于ios - UITableView 不接触单个手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28334216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com