【问题标题】:add tap gesture to a UILabel Programmatically以编程方式将点击手势添加到 UILabel
【发布时间】:2019-02-14 12:17:23
【问题描述】:

我正在尝试在 swift 4 的函数中为动态创建的 UILabel 添加点击手势,但它没有触发 UITapGestureRecognizer 函数。当我从 viewDidLoad 函数添加点击手势时它正在工作,但我必须从其他函数添加点击手势。

这里是代码

    override func viewDidLoad() {

    super.viewDidLoad()

    createLabel()
    }

   func createLabel() {

   let label = UILabel()
   label.text = "abc"
   label.numberOfLines = 0 
   label.frame.size.width = self.otherlinksStack.bounds.width
   label.font = label.font.withSize(17) // my UIFont extension

   label.sizeToFit()
   label.tag = 1
   self.otherlinksStack.addSubview(label)
   let labelTapGesture = UITapGestureRecognizer(target:self,action:#selector(self.doSomethingOnTap))

 label.isUserInteractionEnabled = true

 label.addGestureRecognizer(labelTapGesture)

 }

 @objc func doSomethingOnTap() {

    print("tapped")
}

【问题讨论】:

  • 在问题中添加代码
  • 嗯,您是否将userInteractionEnabled 设置为true?
  • 是的,我将 userInteractionEnabled 设置为 true。但它不起作用
  • 您需要提供更多信息 - 请参阅 How to Ask 和 minimal reproducible example
  • 请查看问题中的代码。我编辑了它。

标签: ios uilabel swift4 uitapgesturerecognizer programmatically-created


【解决方案1】:

你做错了几件事......

// your code
label.frame.size.width = self.otherlinksStack.bounds.width
label.sizeToFit()

如果您将标签添加到 stackView,则无需设置其框架 - 让 stack view 处理。如果您的 stackView 的对齐方式设置为.fill,它无论如何都会将标签拉伸到其宽度。如果它未设置为填充,则标签将根据其文本根据需要水平扩展。因此,也无需致电.sizeToFit()。

// your code
self.otherlinksStack.addSubview(label)

将视图添加到堆栈视图时,请使用.addArrangedSubview,否则它将叠加在堆栈视图中的另一个视图之上。

这应该可以正常工作(在我的快速测试中可以):

func createLabel() {

    let label = UILabel()

    label.text = "abc"
    label.numberOfLines = 0
    label.font = label.font.withSize(17) // my UIFont extension
    label.tag = 1

    // give the label a background color so we can see it
    label.backgroundColor = .cyan

    // enable user interaction on the label
    label.isUserInteractionEnabled = true

    // add the label as an Arranged Subview to the stack view
    self.otherlinksStack.addArrangedSubview(label)

    // create the gesture recognizer
    let labelTapGesture = UITapGestureRecognizer(target:self,action:#selector(self.doSomethingOnTap))

    // add it to the label
    label.addGestureRecognizer(labelTapGesture)

}

@objc func doSomethingOnTap() {
    print("tapped")
}

【讨论】:

    【解决方案2】:

    使用此代码添加点击手势:

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapGestureMethod(_:)))
    tapGesture.numberOfTapsRequired = 1
    tapGesture.numberOfTouchesRequired = 1
    yourLabel.isUserInteractionEnabled = true
    yourLabel.addGestureRecognizer(tapGesture)
    

    这是点击手势方法:

    @objc func tapGestureMethod(_ gesture: UITapGestureRecognizer) {
        Do your code here
    }
    

    【讨论】:

      【解决方案3】:
      func addTapGesture() {
        let labelTapGesture = UITapGestureRecognizer(target: self, action: #selector(doSomethingOnTap))
        //Add this line to enable user interaction on your label 
        myLabel.isUserInteractionEnabled = true
        myLabel.addGestureRecognizer(labelTapGesture)
      }
          
      @objc func doSomethingOnTap() {
        print("tapped")
      }
      

      然后从viewDidLoad 或从您要添加点击的任何功能调用addTapGesture()。

      【讨论】:

      • 不,它不工作。我正在以编程方式创建 UILabel 并在从 viewDidLoad 调用的函数中添加点击手势。
      【解决方案4】:

      如果您的手势通过 viewDidLoad 起作用,那么在从其他函数添加之后,手势可能会被其他控件的手势覆盖。..

      尝试在与所有其他控件分开的控件上添加手势。

      【讨论】:

      • OP 正在创建标签——它还能分离多少?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多