CaffeineShots CaffeineShots - 1 year ago 78
Swift Question

TapGestureRecognizer not firing in swift

Hi I'm having no cards to used to fixed this. Here we go.

I have this five imageview this is the declaration of first imageview but basically they have same declaration

@IBOutlet weak var first: UIImageView!{
first.tag = 1

and Then the tapGesture() method is this

private func tapGesture() -> UITapGestureRecognizer {
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("iconTapped:"))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
return tapGesture

the action when the imageview is tapped the iconTapped() method is called, this is the code

func iconTapped(sender:UITapGestureRecognizer){
if let tag = sender.view?.tag{

I have put all imageview
but still not firing. Could anyone help me?

Some factor may or may not help, inside the
I update constraints and do some animation to imageview, but I'm not adding any subview programmatically

Answer Source

after trying so many things, I look on my view hierarchy and figure out that the parent view where my UIImageView rest userInteractionEnable is set to false(unchecked) in storyboard.

in this case the parentview is my superview, to fixed that I need to add this in viewDidLoad()

self.view.userInteractionEnabled = true


click the parentView in storyboard and in attributes inspector check the User Interaction Enable.

ps. sorry for silly mistake but I'll keep this question posted there might be other encountering same problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download