Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GestureRecognizer not responding to tap

After initialisation of by subclass of UIImageView I have the following line of code:

self.userInteractionEnabled = true
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))

I created the necessary associated function :

func handleTap(gestureRecognizer: UITapGestureRecognizer) {
    print("In handler")
}

On tapping on the view in question, "In handler was never printed to the console". I then removed the handler function to see if the compiler would complain about the missing function. It didn't.

I'm positively stumped. I'd truly appreciate any light people can shed on this.

Update: My class is actually a UIImageView as opposed to UIView

like image 430
Didia Avatar asked Sep 25 '14 00:09

Didia


4 Answers

I was using UITapGestureRecognizer that I placed on a UILabel using Storyboard.

To get this to work I also had to place a checkmark in the block labeled: "User Interaction Enabled" in the UILabel Attributes Inspector in the Storyboard.

enter image description here

like image 176
Scooter Avatar answered Nov 16 '22 22:11

Scooter


I discovered the answer after carefully combing through my code.

One of the parent views was created without supplying a frame:

While it's a noobish enough error to warrant deletion of this questions, odds are someone else will also have the same issue in the future...

like image 22
Didia Avatar answered Nov 16 '22 22:11

Didia


Try this

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.view.userInteractionEnabled = true
    var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
    self.view.addGestureRecognizer(tapGesture)
}

func handleTap(sender : UIView) {
    println("Tap Gesture recognized")
}
like image 18
Suresh Kumar Durairaj Avatar answered Nov 16 '22 22:11

Suresh Kumar Durairaj


In addition to the other answers, this can be caused by adding the gesture recognizer to multiple views. Gesture recognizers are for single views only.

Reference: https://stackoverflow.com/a/5567684/6543020

like image 14
bradkratky Avatar answered Nov 16 '22 23:11

bradkratky