Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 向UILabel添加手势_Ios_Swift_Uilabel_Uigesturerecognizer - Fatal编程技术网

Ios 向UILabel添加手势

Ios 向UILabel添加手势,ios,swift,uilabel,uigesturerecognizer,Ios,Swift,Uilabel,Uigesturerecognizer,我试图在我的UILabel中添加点击手势,但在点击标签时无法使其弹出。我看了看其他人是怎么做的,我发现他们在一行中声明了标签,然后在视图中添加了手势。是否可以在我的标签声明中添加手势以保持整洁 let apetureButton: UILabel = { let tapLabel = UITapGestureRecognizer(target: self, action: #selector(apetureSelect)) let text = UILabel() te

我试图在我的
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")
}


您需要一个lazyvar才能从闭包内部访问
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
}()