首页 > 解决方案 > UILabel TapGesture 未触发

问题描述

知道为什么 UI 标签上的 Tap 手势不会触发吗?

我也尝试过使用委托,但以最简单的形式,由于某种原因,它不会命中操作方法。

UIView 层是否限制了这种交互?

class TestTextViewLabel : UIView {
    weak var testTextView: UITextView!
    weak var testLabel: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(testLabelTapped(_:)))

        let testUITextView: UITextView = {
            let textView = UITextView()
            textView.textColor = UIColor(hex: "#000")
            textView.translatesAutoresizingMaskIntoConstraints = false
            return textView
        }()

        let testUILabel: UILabel = {
            let label = UILabel()
            label.textColor = UIColor(hex: "#666666")!
            label.translatesAutoresizingMaskIntoConstraints = false

            label.addGestureRecognizer(tapGesture)
            label.isUserInteractionEnabled = true

            return label
        }()

        self.addSubview(testUITextView)
        self.addSubview(testUILabel)

        self.testTextView = testUITextView
        self.testLabel = testUILabel

        testUITextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
        testUITextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
        testUITextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true

        testUILabel.topAnchor.constraint(equalTo: testUITextView.bottomAnchor, constant: 50).isActive = true
        testUILabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
        testUILabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func testLabelTapped(_ sender: UITapGestureRecognizer) {
        print("testLabelTapped")
    }
}

标签: iosswiftuilabel

解决方案


我尝试运行您的课程,并且在将文本放入其中后,我能够让 UILabel 触发。点击手势只能在视图的范围内被识别,并且由于您在 UILabel 中没有任何文本,因此边界为零,因此您无法单击它。默认情况下,如果您输入文本,UILabel 将自动匹配这些边界。下面是我的工作代码:

class TestTextViewLabel : UIView {
    weak var testTextView: UITextView!
    weak var testLabel: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(testLabelTapped(_:)))

        let testUITextView: UITextView = {
            let textView = UITextView()
            textView.textColor = UIColor.black
            textView.translatesAutoresizingMaskIntoConstraints = false
            textView.text = "This is a text view"

            textView.backgroundColor = .clear
            return textView
        }()

        let testUILabel: UILabel = {
            let label = UILabel()
            label.textColor = UIColor(red:0.40, green:0.40, blue:0.40, alpha:1.0)
            label.translatesAutoresizingMaskIntoConstraints = false

            label.addGestureRecognizer(tapGesture)
            label.isUserInteractionEnabled = true

            label.text = "This is a UILabel view"

            return label
        }()

        self.addSubview(testUITextView)
        self.addSubview(testUILabel)

        self.testTextView = testUITextView
        self.testLabel = testUILabel

        testUITextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
        testUITextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
        testUITextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true

        testUILabel.topAnchor.constraint(equalTo: testUITextView.bottomAnchor, constant: 50).isActive = true
        testUILabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
        testUILabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func testLabelTapped(_ sender: UITapGestureRecognizer) {
        print("testLabelTapped")
    }
}

推荐阅读