向 UILabel 添加手势

Add gesture to UILabel

我正在尝试向我的 UILabel 添加点击手势,但在点击标签时无法弹出。我看了一下其他人是如何做的,从我发现的那些人中,他们一直在一行中声明标签,然后在视图中添加手势确实加载了。是否可以在我的标签声明中添加手势以保持整洁?

let apetureButton: UILabel = {
    let tapLabel = UITapGestureRecognizer(target: self, action: #selector(apetureSelect))
    let text = UILabel() 
    text.isUserInteractionEnabled = true
    text.addGestureRecognizer(tapLabel)
    text.text = "400"  
    return text
}()

@objc func apetureSelect(sender:UITapGestureRecognizer) {
     print("ApetureSelect")
}

您需要一个 lazy var 才能从闭包

内部访问 self
lazy var apetureButton: UILabel = { 
   let text = UILabel() 
   let tapLabel = UITapGestureRecognizer(target: self, action: #selector(apetureSelect))
   text.isUserInteractionEnabled = true
   text.addGestureRecognizer(tapLabel)
   text.text = "400" 
   return text
}()