简体   繁体   中英

iOS: TapGestureRecognizer in UIView not fired

I have create an date component and have problems with the GestureRecognizer.

在此处输入图片说明

First line is a StackView with labels. If I attach a TapGestureRecognizer it is fired on touch. The following Lines are subviews in a Stackview consisting of a label and an image view. I can add the Recognizer to the subview, to the label or the image view. It never gets fired and I made sure that userInteraction is enabled.

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

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

You need three different objects of UITapGestureRecognizer you can't add same tap gesture on different views

Hope it may solve your problem

For every view ( Label/Imageview ) you should create a tap object not the same one added to both . . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM