首页 > 其他 > 详细

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

时间:2021-07-07 12:47:41      阅读:91      评论:0      收藏:0      [点我收藏+]

父视图手势设置

 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
    }

}

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!