简体   繁体   English

触摸时取消手势识别器 UITableView

[英]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. 如果UIGestureRecognizercancelsTouchesInView属性为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/uigesturerecognizerhttps://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.

相关问题 UIScroll view内的UITableview-触摸tableview时禁用scrollview滚动 - UITableview inside UIScroll view - Disable scroll of scrollview when touching tableview 触摸控件时取消UITableView的滚动 - Cancel scrolling of UITableView when control is touched 触摸时取消UIButton触摸 - Cancel UIButton touch while touching it 初始化手势时查找目标 - Finding Target when initializing gestureRecognizer 通过触摸 UITableView 的背景关闭键盘 - Dismiss keyboard by touching background of UITableView 如何在UITableView的框架中而不是边界中获取gestureRecognizer的位置 - How to get location of gestureRecognizer in UITableView's frame, not bounds 当用户点击或滚动UITableView时如何取消功能? - How to cancel a function when a user taps on or scrolls through a UITableView? UITableView的第0部分的标题视图未调用GestureRecognizer或Button Action - GestureRecognizer or Button Action not getting called for header view of section 0 of UITableView 接收gestureRecognizer:当gestureRecognizerShouldBegin开始时应与GestureRecognizer同时识别:否 - Receiving gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer when gestureRecognizerShouldBegin: returned NO 当我单击文本中的URL时,GestureRecognizer无法正常工作 - GestureRecognizer is not working when i click on URL in text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM