6

Is it possible to handle touch events in the key UIWindow in the app Delegate or anywhere else?

Any help would be appreciated please.

4 Answers 4

10

There is a handy catch-all method in UIWindow called sendEvent: which sees every event near the start of the event-handling pipeline. If you want to do any non-standard additional event handling, this is a good place to put it. Something like this:

- (void)sendEvent:(UIEvent *)event {
  if ([self eventIsNoteworthy:event]) [self extraEventHandling:event];
  [super sendEvent:event];  // Apple says you must always call this!
}

Docs: UIWindow class reference | iOS event delivery docs

This blog post also mentions how to override hitTest:withEvent: to catch some events before they're bound to the target leaf subview in the view hierarchy. You can also override that method on your UIWindow object if you want.

4

You will have to subclass UIWindow with your own class and override sendEvent: method. But remember that the method gets other types of events - not only touches so you have to check for event type (event.type == UIEventTypeTouches). Also since you get set of touches you might want to check which ones just began, which ones ended, moved etc. To do that you have to iterate through allTouches and check the phase property of every UITouch.

@implementation TouchWindow

- (void)sendEvent:(UIEvent *)event {

    if (event.type == UIEventTypeTouches) {
        for(UITouch * t in [event allTouches]) {
            if(t.phase == UITouchPhaseBegan) {
                /*
                Paste your code here. 
                Inform objects that some touch has occurred. 
                It's your choice if you want to perform method/selector directly, 
                use protocols/delegates, notification center or sth else. 
                */
            }
        }
    }

    [super sendEvent:event];
}

@end

Of course TouchWindow is subclass of UIWindow

@interface TouchWindow : UIWindow 
@end

And you will probably have to change that class in your .xib file in XCode

3

UIWindow is a subclass of UIView, so you simply subclass it and use it in you AppDelegate:

self.window = [[MyWindow alloc] initWithFrame:CGRectMake(0,0,320,480)]; // Sorry for hard-coded frame size!

and in MyWindow you override -hitTest:withEvent: and/or -pointInside:withEvent:

0

UIWindow is a subclass of UIResponder, which has APIs for handling touch events (e.g., touchesBegan:withEvent:). It is possible then for you to subclass UIWindow, override the touch event handling APIs, and manage the touch events yourself.

1
  • 1
    If UIWindow has some subviews similar to UITableView, last catches all touch events before window has chance to handle them. So touchesBegan:withEvent: group of methods of window is never called in this circumstances. In this case sendEvent: of UIWindow is the only way to handle touch events before table view eats them. Jan 17, 2013 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.