Search code examples
objective-ciosipaduitouch

Detecting all touches in an app


In an iPad app, wherever a user is touching the screen I want to display an image, highlighting the points they are touching. The app contains a number of nested views, all of which should receive touches and behave normally.

Seems simple, but I've failed to find a good way to do it. Using the touches began: with event and related functions on the root view controller does not work because if a subview is touched the events are not fired. I've also created a 'dummy' gesture recognizer which just passes touch events to another class which draws images. That works great-ish and buttons work, but breaks UIScrollViews and I'm guessing other subviews with gesture reconizers.

Is there nowhere you can just access all touch events without affecting where those touches are headed?

thanks.


Solution

  • Your dummy gesture recognizer should be ok. Just watch out for setting states. possible -> began -> ...

    Basically your gesture recognizer is forwarding all touches so it can be in began or possible state all the time while any touch exists.

    To get rid of problems with other gesture recognizers return YES in this delegate method.

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
    

    Other option is to subclass main UIWindow in your app and override this method

    - (void)sendEvent:(UIEvent *)event
    

    Here you should have access to all events. It's quite easy to filter them.