I have a UIView and and I have added tap gesture to it:
let tap = UITapGestureRecognizer(target: self, action: Selector(\"handleTap:\"))
tap.delegate = self
Swift 4
First, create an object of UITapGestureRecognizer
var tapGesture = UITapGestureRecognizer()
The second step is to initialise UITapGestureReconizer. Enable the user interaction, then add it.
override func viewDidLoad() {
super.viewDidLoad()
tapGesture = UITapGestureRecognizer(target: self, action: #selector(YourViewController.myviewTapped(_:)))
infosView.isUserInteractionEnabled = true
infosView.addGestureRecognizer(tapGesture)
view.addSubview(infosView)
}
Third, create a method
@objc func myviewTapped(_ recognizer: UIGestureRecognizer) {
print("button is tapped")
}