How To Check If User Touched Anything In Your I Os App
So you want to know when user is interacting with your app and you don’t really care which screen is he using or what is he doing. You only want to know when he touches your app, for example: you make a game and you would like to automatically go into pause mode if user didn’t use your app for N minutes…
The solution that I will describe also can be used to find if or where UIWebView was touched without interfering with the web clickable content, for example showing an overlay where the user touched your UIWebView content…
Implement a simple solution
Doing that at each top view level would be waste of time and not something that you really need.
You could add some custom subview to your window, intercept touches there and try to somehow pass it along to real views that needs it ( after all you can have many interactive views ). But this is not simple, it requires subclassing, adding custom views to windows…
Actually UIWindow is subclass of UIView, that means you can add a gesture recognizer to it. But how to make sure this will not have influence on real content of your app ?
Make AppDelegate the delegate of UIGestureRecognizer that you just added, and override shouldReceiveTouch as follows:
1 2 3 4 5
This allows you to intercept any screen touches but ignores it and prevents your code from influencing your app normal behavior ( UIControls or other UIGestureRecognizers works as always ) .
Adding UIGestureRecognizer to arbitrary view, be it UIWindow or UIWebView and setting it’s delegate to return NO on shouldReceiveTouch allows you to execute arbitrary code when user puts down its finger on screen without interfering with UIControls or UIGestureRecognizers behaviors.
I’m merowing_ on twitter if you feel like talking about it.