[英]iOS: TapGestureRecognizer in UIView not fired
I have create an date component and have problems with the GestureRecognizer. 我创建了一个日期组件,并且在GestureRecognizer中遇到了问题。
First line is a StackView with labels. 第一行是带有标签的StackView。 If I attach a TapGestureRecognizer it is fired on touch.
如果我连接了TapGestureRecognizer,则会在触摸时触发。 The following Lines are subviews in a Stackview consisting of a label and an image view.
以下几行是Stackview中由标签和图像视图组成的子视图。 I can add the Recognizer to the subview, to the label or the image view.
我可以将Recognizer添加到子视图,标签或图像视图。 It never gets fired and I made sure that userInteraction is enabled.
它永远不会被解雇,我确保已启用userInteraction。
What could be the problem? 可能是什么问题呢?
Here an example how I add the recognizer: 这是我添加识别器的示例:
func addTap(){
dayLabel.isUserInteractionEnabled = true
imageView.isUserInteractionEnabled = true
isUserInteractionEnabled = true
tap1 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
tap2 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
tap3 = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:)))
dayLabel.addGestureRecognizer(tap1!)
imageView.addGestureRecognizer(tap2!)
addGestureRecognizer(tap3!)
}
@objc func handleTap(recognizer: UITapGestureRecognizer){
logger.debug("tap")
changeSelection()
}
You can find a little demo project boiled down to the problem here: https://github.com/ogezue/datedemo 您可以在此处找到一个归结为问题的小演示项目: https : //github.com/ogezue/datedemo
Problem I can see is your view must be cover with Image and label and you are adding same UITapGestureRecognizer
to all so that is added on last object say view (which is covered with image and label) so it may not able to get tap event 我看到的问题是,您的视图必须覆盖有图像和标签,并且您
UITapGestureRecognizer
所有视图添加相同的UITapGestureRecognizer
,以便将其添加到最后一个对象说视图(覆盖有图像和标签),因此它可能无法获得点击事件
You need three different objects of UITapGestureRecognizer
you can't add same tap gesture on different views 您需要
UITapGestureRecognizer
三个不同的对象,您不能在不同的视图上添加相同的点击手势
Hope it may solve your problem 希望它可以解决您的问题
For every view ( Label/Imageview ) you should create a tap object not the same one added to both . 对于每个视图( 标签/图像视图),您都应该创建一个水龙头对象,而不是将两者同时添加到其中。 .
。 .
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.