Anthony Mattox
Anthony Mattox

Reputation: 7118

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.

Upvotes: 3

Views: 2853

Answers (3)

Split
Split

Reputation: 4329

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.

Upvotes: 6

Mark Adams
Mark Adams

Reputation: 30846

You can apply a UITapGestureRecognizer to the entire view and set the cancelsTouchesInView property to NO. This will allow you to be notified of all taps on the view and its subviews without intercepting all of the touch events.

Additionally, you can implement the -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: delegate method to keep this gesture recognizer from stomping on the ones used by views like UIScrollView.

Upvotes: 3

tiltem
tiltem

Reputation: 5042

you can try overriding hitTest:withEvent:

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event

This maybe what you are looking for.

Upvotes: 1

Related Questions