[英]Add a UITapGestureRecognizer to a subview of UIPickerView
Im having problems receiving a UITapGesture
on a UILabel
which is a subview of a UIPickerView
. 我在
UILabel
上接收UITapGesture
时遇到问题, UILabel
是UIPickerView
的子视图。
let statusDisplayLabel = UILabel(frame: CGRect(x: 0,
y: 0,
width: <#width of UIPickerView #>,
height: <# height of UIPickerView row #>))
statusDisplayLabel.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self,
action: #selector(didTapStatusDisplayLabel(tap:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
statusDisplayLabel.addGestureRecognizer(tapGesture)
<# UIPickerView #>.addSubview(statusDisplayLabel)
@objc func didTapStatusDisplayLabel(tap: UITapGestureRecognizer){
print("tapped")
}
The UILabel
displays as it should over the UIPickerView
. UILabel
在UIPickerView
显示为应显示的状态。 However "tapped" message is never displayed in console as the tap just falls through the statusDisplayLabel
and moves the UIPickerView
instead. 但是,“轻击”消息永远不会在控制台中显示,因为点击仅落在
statusDisplayLabel
,而是移动UIPickerView
。 It's as if the UILabel
is not there 好像
UILabel
不存在
What am I missing? 我想念什么?
尝试创建Container View ,在该视图中放入UIPickerView,并尝试将标签添加到Container View中,而不是将其添加到选择器视图中。
Check following 检查以下
First check view hierarchy, is label on top of picker view 第一个检查视图层次结构,标签位于选择器视图的顶部
if all correct then 如果一切正确的话
Here, missing part is only userInteractionEnabled
在这里,缺少的部分只是
userInteractionEnabled
EDIT 编辑
You can debug view hierarchy using 您可以使用以下命令调试视图层次结构
EDIT2 EDIT2
You can implement func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
您可以实现
func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
in subclass of UIView
as alternative solution. 在
UIView
子类中作为替代解决方案。 If you need demo then I can post 如果您需要演示,那么我可以发布
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.