Can we Add Tap Gesture to UILabel?

 ̄綄美尐妖づ 提交于 2019-12-09 03:58:50

问题


I have added Label in the UIView and created its outlet, then with the help of UITapGestureRecognizer i have added the functionality,but when i tap or click in the label. Label text does not changes. I searched for the similar question and i got the answer also its what I've written but still the label text is not changing. Code written in Swift 3.0. Here's the code i have written -

 import UIKit

class testingViewController: UIViewController {

    @IBOutlet weak var testLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
      let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showDatePicker))
      tap.numberOfTouchesRequired = 1
      tap.numberOfTapsRequired = 1
      testLabel.addGestureRecognizer(tap)
      testLabel.isUserInteractionEnabled = true// after adding this it worked
        // Do any additional setup after loading the view.
    }

  func showDatePicker(){
    print("testing")
  }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

回答1:


Yes you can, but you need to set the label's isUserInteractionEnabled flag to true



来源:https://stackoverflow.com/questions/42489148/can-we-add-tap-gesture-to-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!