ios - UILabel TapGesture 未触发
问题描述
知道为什么 UI 标签上的 Tap 手势不会触发吗?
我也尝试过使用委托,但以最简单的形式,由于某种原因,它不会命中操作方法。
UIView 层是否限制了这种交互?
class TestTextViewLabel : UIView {
weak var testTextView: UITextView!
weak var testLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(testLabelTapped(_:)))
let testUITextView: UITextView = {
let textView = UITextView()
textView.textColor = UIColor(hex: "#000")
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
let testUILabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(hex: "#666666")!
label.translatesAutoresizingMaskIntoConstraints = false
label.addGestureRecognizer(tapGesture)
label.isUserInteractionEnabled = true
return label
}()
self.addSubview(testUITextView)
self.addSubview(testUILabel)
self.testTextView = testUITextView
self.testLabel = testUILabel
testUITextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
testUITextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUITextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
testUILabel.topAnchor.constraint(equalTo: testUITextView.bottomAnchor, constant: 50).isActive = true
testUILabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUILabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func testLabelTapped(_ sender: UITapGestureRecognizer) {
print("testLabelTapped")
}
}
解决方案
我尝试运行您的课程,并且在将文本放入其中后,我能够让 UILabel 触发。点击手势只能在视图的范围内被识别,并且由于您在 UILabel 中没有任何文本,因此边界为零,因此您无法单击它。默认情况下,如果您输入文本,UILabel 将自动匹配这些边界。下面是我的工作代码:
class TestTextViewLabel : UIView {
weak var testTextView: UITextView!
weak var testLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(testLabelTapped(_:)))
let testUITextView: UITextView = {
let textView = UITextView()
textView.textColor = UIColor.black
textView.translatesAutoresizingMaskIntoConstraints = false
textView.text = "This is a text view"
textView.backgroundColor = .clear
return textView
}()
let testUILabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(red:0.40, green:0.40, blue:0.40, alpha:1.0)
label.translatesAutoresizingMaskIntoConstraints = false
label.addGestureRecognizer(tapGesture)
label.isUserInteractionEnabled = true
label.text = "This is a UILabel view"
return label
}()
self.addSubview(testUITextView)
self.addSubview(testUILabel)
self.testTextView = testUITextView
self.testLabel = testUILabel
testUITextView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
testUITextView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUITextView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
testUILabel.topAnchor.constraint(equalTo: testUITextView.bottomAnchor, constant: 50).isActive = true
testUILabel.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
testUILabel.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func testLabelTapped(_ sender: UITapGestureRecognizer) {
print("testLabelTapped")
}
}
推荐阅读
- java - 注入 bean 返回 null Java EE 8
- three.js - Three.js sprite sheet 在 THREE.Points 中的偏移量
- java - 如何在另一个集合中搜索一个集合的项目
- angular - 如何在一个离子应用程序中创建两个不同的离子菜单?
- sql - 从帖子中获取 ID 以将评论连同评论一起发送到数据库的最佳方式
- c# - 将 WorldToScreenPoint 与移动相机一起使用时出现奇怪的移动
- excel - 在VBA中找到字符出现在单元格范围内的单个单元格中的最大次数
- c# - 如何在 C# 中对图像应用“内斜角”效果?最好使用 ImageSharp 库
- reactjs - 升级到 Emotion v11 后 Stylis 编译器错误
- python - 使用 Python 在 PySpark 上查询 SQL 失败 - 未找到表或视图