gpt4 book ai didi

ios - 向 UILabel 添加手势

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:17 24 4
gpt4 key购买 nike

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

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
}()

关于ios - 向 UILabel 添加手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56709605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com