简体   繁体   English

iOS:未触发UIView中的TapGestureRecognizer

[英]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.

相关问题 TapGestureRecognizer 在 UIView 子视图中不起作用 - TapGestureRecognizer not working in UIView subviews 使用xib将tapGestureRecognizer添加到自定义UIView - Adding a tapGestureRecognizer to a Custom UIView with xib iOS:TapGestureRecognizer 问题 - iOS: TapGestureRecognizer Issues 将tapGestureRecognizer添加到UIView中的UIImageView并按协议调用方法 - add tapGestureRecognizer to UIImageView in UIView and call method by protocol iOS - UITapGestureRecognizer未在UITcrollView内部UITableViewCell内的UIView中触发 - iOS - UITapGestureRecognizer not fired from UIView inside UIScrollView inside UITableViewCell iOS TapGestureRecognizer-多点触控实现 - iOS TapGestureRecognizer — Multiple Touches Implementation Xamarin表格-TapGestureRecognizer无法在iOS上使用 - Xamarin Forms - TapGestureRecognizer not working for iOS iOS TapGestureRecognizer会对numberOfTapsRequired = 1进行双击 - iOS TapGestureRecognizer eats double tap for numberOfTapsRequired=1 iOS-TapGestureRecognizer-Tap适用于整个屏幕而不适用于视图 - iOS - TapGestureRecognizer - Tap is applicable for the whole screen not for a view 以编程方式链接的基于UIView块的动画在iOS上首次触发时挂起 - Programmatically linked UIView block-based animations hang at the first time fired on iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM