简体   繁体   中英

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.

can we actually add gesture recognizer to UI Label? or what should I do to add gesture to the UI label?

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

Certainly. But you would also need to turn on the label's isInteractionEnabled if you want the gesture recognizer to do anything.

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 .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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