user7804097
user7804097

Reputation: 308

swift UITapGestureRecognizer not working on view

I had to create a new thread bcoz it's driving me crazy and all the other answers online are exactly the same. I have done this countless of times but I cannot see what I am missing for the life of me. I am using a "test" view controller just to get the tap gesture working but it isn't working at all... I am fairly certain that I am setting this up correctly, as this is how I've always implemented it in the past: (yes, I have checked the box for isUserInteractionEnabled). I am even implementing this on a different viewcontroller this exact way and it is working...

class TestViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    view.addGestureRecognizer(tap)
  }

  let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

    
  @objc func wasTapped() {
    print("tapped")
  }
}

I have also tried adding the parameters to wasTapped:

@objc func wasTapped(gestureRecognizer: UITapGestureRecognizer) {
  print("tapped")
}

Upvotes: 6

Views: 17428

Answers (4)

Mr.Javed Multani
Mr.Javed Multani

Reputation: 13294

var tapGesture = UITapGestureRecognizer()

take a view and set IBOutlet like:

@IBOutlet weak var viewTap: UIView!     

Write pretty code on viewDidLoad() like:

tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.myviewTapped(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
viewTap.addGestureRecognizer(tapGesture)
viewTap.isUserInteractionEnabled = true

this method is calling when tap gesture recognized:

@objc func myviewTapped(_ sender: UITapGestureRecognizer) {
  if self.viewTap.backgroundColor == UIColor.yellow {
    self.viewTap.backgroundColor = UIColor.green
  }else{
    self.viewTap.backgroundColor = UIColor.yellow
  }
}

Upvotes: 1

Mannopson
Mannopson

Reputation: 2684

Try something like this

override func viewDidLoad() {
    super.viewDidLoad()

     let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped(sender:)))
     tap.numberOfTapsRequired = 1 // Default value
     view.isUserInteractionEnabled = true
     view.addGestureRecognizer(tap)

}

@objc func wasTapped(sender: UITapGestureRecognizer) {
    print("tapped")
}

Upvotes: 9

matt
matt

Reputation: 536027

You are saying:

override func viewDidLoad() {
    super.viewDidLoad()

    view.addGestureRecognizer(tap)
}

let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

The problem is the last line:

let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))

You cannot just say let tap like that in the middle of nowhere. You are implicitly making an instance property. But you cannot initialize an instance property with a target of self, because self does not exist at the time an instance property is initialized. (I regard the fact that that code even compiles as a bug, and have reported it as such.)

Move that line to the start of viewDidLoad, like this:

override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(wasTapped))
    view.addGestureRecognizer(tap)
}

Upvotes: 15

MQLN
MQLN

Reputation: 2328

You have to enable interaction if you want to use gesture recognizers for standard UIView's

Add view.isUserInteractionEnabled = true in your viewDidLoad.

Upvotes: 5

Related Questions