在自定义view
中添加了点击手势,那点击UICollectionView
、 UITableView
的cell
时不响应其didSelect
代理方法,原因是手势冲突了
2种方法
- 给view设置
restorationIdentifier
let lbTap = UITapGestureRecognizer(target: self, action: #selector(xxx))
lbTap.delegate = self
vBg.addGestureRecognizer(lbTap)
vBg.restorationIdentifier = "vBg"
- 在手势代理中判断
view
的类型
//
// MARK: - cell item点击和 vBg的点击冲突
//
extension XXXView: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
///第一种 1.设置view的identify
if let identify = touch.view?.restorationIdentifier {
return identify == "vBg"
}
///第二种 2.判断view的类型
let clsTouch = NSStringFromClass((touch.view?.classForCoder)!)
var clsSuper = ""
if let supCls = touch.view?.superview, let suppCls = supCls.superview {
clsSuper = NSStringFromClass(suppCls.classForCoder)
} else {
return true
}
///UICollectionView UITableView就拦截点击事件
if clsTouch.hasPrefix("UITableView") || clsTouch.hasPrefix("UICollectionView") ||
clsSuper.hasPrefix("UITableView") || clsSuper.hasPrefix("UICollectionView") {
return false
}
return true
}
}
网友评论