【问题标题】:Add gesture to UILabel向 UILabel 添加手势
【发布时间】:2019-06-21 19:48:20
【问题描述】:

我正在尝试向我的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")
}

【问题讨论】:

    标签: ios swift uilabel uigesturerecognizer


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多