gpt4 book ai didi

ios - GestureRecognizer 不适用于 TableView

转载 作者:搜寻专家 更新时间:2023-11-01 07:28:25 27 4
gpt4 key购买 nike

我正在尝试找到一种方法,当用户在过滤我的 tableview 的内容后点击屏幕上的其他地方时,立即隐藏键盘。

但不知何故,当我添加 UITapGestureRecognizer 时,我的 TableView 卡住了,无法进一步进入应用程序。

这是我的做法:

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tap)

func dismissKeyboard() {

view.endEditing(true)
}

我已经在带有 TextField 的 Blank ViewController 上尝试过这个并且它有效。我错过了什么?有没有一种特定的方法可以将 GestureRecognizer 添加到 TableView 中?因为一旦我添加手势,它就会破坏一切。我也尝试使用 resignFirstResponder 得到相同的结果。

有什么建议吗?谢谢!

最佳答案

尝试将 cancelsTouchesInView 变量设置为 false。这默认为 true,通过将其设置为 false,您允许通过手势响应触摸,然后通过手势返回 View ,以便 tableView 也可以响应。

tap.cancelsTouchesInView = false

Documentation on Apple

文档中的讨论摘录:

When this property is true (the default) and the receiver recognizes its gesture, the touches of that gesture that are pending are not delivered to the view and previously delivered touches are cancelled through a touchesCancelled:withEvent: message sent to the view. If a gesture recognizer doesn’t recognize its gesture or if the value of this property is false, the view receives all touches in the multi-touch sequence.

关于ios - GestureRecognizer 不适用于 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34358762/

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