netshark1000
netshark1000

Reputation: 7413

iOS: TapGestureRecognizer in UIView not fired

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

enter image description here

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

Upvotes: 0

Views: 82

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100541

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

Upvotes: 0

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16466

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

Upvotes: 1

Related Questions