I have multiple views defined in my main view. I want to add single tap gesture to all these views. Below is the code I have written, but this registers a tap gesture to the
// Add tap gesture in swift 4
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
view.addGestureRecognizer(tap)
view.isUserInteractionEnabled = true
self.view.addSubview(view)
// function which is triggered when handleTap is called
@objc func handleTap(_ sender: UITapGestureRecognizer) {
if sender.view == view1 {
// do something
}
else{
// do next thing
}
}
//USE in view did load: tapGestures(view: view1) tapGestures(view: view2) tapGestures(view: view3)