Is there a way to detect any tap outside the current view? I unsuccessfully tried to implement something with the hitTest method but I am not sure to understand it well.
-
have you tried touchesBegan: method – ankit Apr 19 '17 at 11:51
-
You may want to add a tapGesture on the view containing the current view. – Larme Apr 19 '17 at 11:52
-
I'm doing this: https://stackoverflow.com/a/48790850/7132300 – algrid Feb 14 '18 at 15:44
2 Answers
What you have to do is, In touchesBegan you have to get the first touch object from the touches set and you have to check the location of that touch in a view(inside which you want to detect the touch).
After you get the location of touch in View, you have to check whether your currentView(The view which you have to check whether tap was inside or outside).
If currentView's frame contains the touch location, that means touch has occurred inside the view otherwise outside.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let touch = touches.first
guard let location = touch?.location(in: self.view) else { return }
if !currentView.frame.contains(location) {
print("Tapped outside the view")
} else {
print("Tapped inside the view")
}
}
Hope it Helps!

- 9,188
- 10
- 67
- 113

- 2,873
- 17
- 32
-
-
-
Yes, but, it works ok regardless for a quick test. I was thinking that maybe there are some cases in which not calling `super` would cause issues, because [the documentation](https://developer.apple.com/documentation/uikit/uiresponder/1621142-touchesbegan?language=objc) states this also: **When creating your own subclasses, call super to forward any events that you do not handle yourself.** – Iulian Onofrei Jun 29 '20 at 14:38
You can use UIGestureRecognizerDelegate protocol
extension YourViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldReceive touch: UITouch) -> Bool {
return (touch.view === self.view)
}
}
This will only returns "true" when the touch was on the background view but "false" if it was inside the your View.
Note : The identity operator === to compare touch.view with self.view. You want to know whether both variables refer to the same object.
And in viewDidLoad() you will create the gestureRecognizer and set delegate.
let gestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(yourActionMethod))
gestureRecognizer.cancelsTouchesInView = false
gestureRecognizer.delegate = self
view.addGestureRecognizer(gestureRecognizer)

- 468
- 3
- 17
-
2This is what I need, very short and works perfectly for me! Thank you. – iAleksandr Aug 29 '18 at 22:01