[英]Cancel gesturerecognizer when touching UITableView
I have a view with 2 tableview and several textfield, I have implemented an extension to hide the keyboard:我有一个带有 2 个 tableview 和几个文本字段的视图,我已经实现了一个隐藏键盘的扩展:
The keyboard hides when I touch the screen but I would like to disable UITapGestureRecognizer when I touch the TableView, otherwise I can not interact with the cells.当我触摸屏幕时键盘隐藏,但我想在触摸 TableView 时禁用 UITapGestureRecognizer,否则我无法与单元格交互。
extension UIViewController {
func OcultarTecladoTocarPantalla() {
let tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.OcultarTeclado))
view.addGestureRecognizer(tap)
}
@objc func OcultarTeclado() {
view.endEditing(true)
}
} }
EDIT: I see that you are extending UIViewController (which may not be advisable because it applies to all instances and subclasses of UIViewController), but you should still be able to set up the delegate. 编辑:我看到您正在扩展UIViewController(这可能是不可取的,因为它适用于UIViewController的所有实例和子类),但是您仍然应该能够设置委托。 Make the following changes (code is pulled from this answer ).
进行以下更改(从此答案中提取代码)。
Extend UIGestureRecognizerDelegate
by writing extension UIViewController: UIGestureRecognizerDelegate {
通过编写
extension UIViewController: UIGestureRecognizerDelegate {
扩展UIGestureRecognizerDelegate
extension UIViewController: UIGestureRecognizerDelegate {
Set the delegate when you add the gesture recognizer: tap.delegate = self
添加手势识别器时设置委托:
tap.delegate = self
Implement the following delegate method. 实现以下委托方法。 It will need to be modified slightly to handle two tableviews.
需要稍作修改以处理两个表视图。
// UIGestureRecognizerDelegate method
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view?.isDescendantOfView(self.tableView) == true {
return false
}
return true
}
Previous answer: 先前的答案:
If the cancelsTouchesInView
property of UIGestureRecognizer
is false the view underneath it will receive touches in addition to the gesture recognizer. 如果
UIGestureRecognizer
的cancelsTouchesInView
属性为false,则其下的视图除了手势识别器之外还将接收触摸。
If you do want to effectively disable the gesture for that case, implement the delegate method gestureRecognizer(_:shouldReceive:)
and return false if the touch is in the tableview. 如果确实要在这种情况下有效禁用手势,请实现委托方法
gestureRecognizer(_:shouldReceive:)
,如果触摸在表gestureRecognizer(_:shouldReceive:)
,则返回false。
See https://developer.apple.com/documentation/uikit/uigesturerecognizer and https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624214-gesturerecognizer . 请参阅https://developer.apple.com/documentation/uikit/uigesturerecognizer和https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate/1624214-gesturerecognizer 。
For Swift 5: (Don't forget to set delegate of your tap gesture instance)对于 Swift 5:(不要忘记设置点击手势实例的委托)
extension MyViewController: UIGestureRecognizerDelegate{
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view?.isDescendant(of: self.mTableView) == true {
return false
}
return true
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.