以编程方式为UILabel添加轻拍手势

4
我将尝试为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")
}

2
Add code in to question - Pratik Sodha
3
е•љзЪДпЉМдљ†жКК userInteractionEnabled иЃЊзљЃдЄЇ true дЇЖеРЧпЉЯ - crizzis
是的,我将 userInteractionEnabled 设置为 true。但它不起作用。 - Abdul Qayyum
你需要提供更多的信息 - 请参考[ask]和[mcve]。 - DonMag
1
请查看问题中的代码。我已经编辑过了。 - Abdul Qayyum
显示剩余8条评论
4个回答

5

你做了几件事情不对...


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

如果您将标签添加到stackView中,则无需设置其框架 - 让stackView处理它。如果您的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")
}

1
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调用的函数中添加了点击手势。 - Abdul Qayyum

1
使用以下代码来添加“Tap Gesture”手势:

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
}

0
如果您的手势从viewDidLoad起作用,那么在从其他函数添加手势后,手势可能会被其他控件的手势覆盖。
尝试在与所有其他控件分离的控件上添加手势。

OP正在创建标签 - 它还能更分离吗? - Caleb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接