简体   繁体   English

如何在UI Label中添加手势识别器?

[英]how to add gesture recognizer to UI Label?

在此输入图像描述

I am trying to add tap gesture to the February label like the picture above, but I don't know why when I add gesture recognizer to the UI Label, it doesn't work, but if I assign that tap gesture to the view (the blue box) , it worked as I am expected. 我正在尝试向2月标签添加点击手势,如上图所示,但我不知道为什么当我向UI标签添加手势识别器时,它不起作用,但如果我将该点击手势指定给视图(蓝盒子,它按照我的预期工作。

can we actually add gesture recognizer to UI Label? 我们真的可以在UI Label中添加手势识别器吗? or what should I do to add gesture to the UI label? 或者我该怎么做才能在UI标签上添加手势?

class AttendanceListVC: UIViewController {

    @IBOutlet weak var dummyView: UIView!
    @IBOutlet weak var monthLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()


        prepareTapGestureToChooseMonth()


    }

}



    func prepareTapGestureToChooseMonth() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(AttendanceListVC.tapFunction(sender:)))
        tapGesture.numberOfTapsRequired = 1


        monthLabel.addGestureRecognizer(tapGesture)
        dummyView.addGestureRecognizer(tapGesture)
    }

    @objc func tapFunction(sender: UITapGestureRecognizer) {


       // show pop up

    }


}

can we actually add gesture recognizer to UI Label 我们可以实际添加手势识别器到UI标签

Certainly. 当然。 But you would also need to turn on the label's isInteractionEnabled if you want the gesture recognizer to do anything. 但是如果你想让手势识别器任何事情,你还需要打开标签的isInteractionEnabled

monthLabel.addGestureRecognizer(tapGesture)
monthLabel.isUserInteractionEnabled = true

(Be warned, however, that this may not be a good user interface. Users will not expect to be able to tap a label, and it doesn't respond in a lively visible way to being tapped. Perhaps "February" in your interface should be the title of a button .) (但是,请注意,这可能不是一个好的用户界面。用户不希望能够点击标签,并且它不会以生动的可见方式响应被点击。也许你的界面中的“二月”应该是按钮的标题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM