YYlabel 事件和父视图手势冲突,highlightTapAction无反应

父视图手势设置

 forwardView.isUserInteractionEnabled = true
 let tap = UITapGestureRecognizer(target: self, action: #selector(forwardViewTapClick))
 tap.cancelsTouchesInView = false
 tap.delaysTouchesBegan = false
 tap.delegate = self
 self.forwardView.addGestureRecognizer(tap)

子视图YYLabel的highlightTapAction

forwardContentLabel.highlightTapAction = {[weak self] (containerView, text, range, rect) in
            if let strongSelf = self {
                let attribute = strongSelf.forwardContentLabel.textLayout?.text
                let highlight = attribute?.attribute(YYTextHighlightAttributeName, at: UInt(range.location)) as? YYTextHighlight
                
                guard let userInfo = highlight?.userInfo as? [String: Any] else { return }
              
            }
        }
        self.forwardView.addSubview(forwardContentLabel)

实现手势代理

extension PostView: UIGestureRecognizerDelegate{
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        var isCanRecognize = true   //是否能被识别
        
        if ((touch.view as? YYLabel) != nil) {
            let label = touch.view as! YYLabel
            let attributedString = label.textLayout?.text
            let index : NSInteger? = label.textLayout?.textRange(at: touch.location(in: label))?.start.offset
            if index != nil, index! > 0 {
                let highlight = attributedString?.attribute(YYTextHighlightAttributeName, at: UInt(index!))
                isCanRecognize = highlight != nil ? false : true
            }
        }
        return isCanRecognize
    }

}
原文地址:https://www.cnblogs.com/leqoqo/p/14980389.html