本文通过实际测试,解决了 Swift UICollectionView、UITableView列点击事件与 UITapGestureRecognizer 点击事件的冲突问题,亲测有效。
在 添加UITapGestureRecognizer 事件 指定委托,在shouldReceivetouch 委托中作判断处理,满足要求 返回 true,否则返回fasle。具体参考代码如下:
1、添加事件指定委托
let tapAction = UITapGestureRecognizer.init(target: self,action: #selector(hideKeyBoardAction(sender:))) tapAction.delegate = self self.view.addGestureRecognizer(tapAction)
2、实现委托
//MARK: - UIGestureRecognizerDelegate func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,shouldReceive touch: UITouch) -> Bool { let touchClass = NSStringFromClass((touch.view?.classForCoder)!) let supClass = NSStringFromClass((touch.view?.superview!.superview?.classForCoder)!) if touchClass == "UITableView" || touchClass == "UICollectionView" || supClass == "UITableView" || supClass == "UICollectionView" { return false } return true }如上代码,参考了众多相关博客,尝试得来。为的让同行少走弯路 原文链接:https://www.f2er.com/swift/321788.html