UILabel TapGesture没有开火

问题描述 投票:1回答:1

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

我也尝试过使用委托,但最简单的形式,由于某种原因,它不会触及action方法。

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")
    }
}
ios swift uilabel
1个回答
1
投票

我尝试了运行你的课程,并且在将文本放入其中之后我能够将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")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.