我们可以将Tap Gesture添加到UILabel吗? [英] Can we Add Tap Gesture to UILabel?

查看:80
本文介绍了我们可以将Tap Gesture添加到UILabel吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIView中添加了Label,并创建了它的出口,然后在UITapGestureRecognizer的帮助下,我添加了功能,但是当我点击或单击Label时.标签文本不会更改.我搜索了类似的问题,得到的答案也就是我所写的内容,但标签文本仍未更改.用Swift 3.0编写的代码. 这是我编写的代码-

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.
    }
    */

}

推荐答案

是的,但是您需要将标签的isUserInteractionEnabled标志设置为true

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

这篇关于我们可以将Tap Gesture添加到UILabel吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆