swift UICollectionView UITableVi

2020-03-06  本文已影响0人  喵喵粉

在自定义view中添加了点击手势,那点击UICollectionViewUITableViewcell时不响应其didSelect代理方法,原因是手势冲突了

2种方法

let lbTap = UITapGestureRecognizer(target: self, action: #selector(xxx))
lbTap.delegate = self
vBg.addGestureRecognizer(lbTap)
vBg.restorationIdentifier = "vBg"
//
// 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
    }
}
上一篇 下一篇

猜你喜欢

热点阅读