How to respond to touch events in UIWindow?

Can I handle touch events in a UIWindow key in the Delegate app or elsewhere?

Any help would be appreciated please.

+6
objective-c iphone
source share
4 answers

There is a convenient catch-all method in UIWindow called sendEvent: that sees every event near the start of the event pipeline. If you want to perform any non-standard additional event processing, this is a good place to post it. Something like that:

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

Documents: Link to the UIWindow Class | IOS Delivery Documents

This blog post also mentions how to override hitTest:withEvent: to catch some events before they snap to the subtype of the target sheet in the view hierarchy. You can also override this method in your UIWindow object if you wish.

+10
source share

You will have to subclass UIWindow with your own class and override the sendEvent: method. But remember that the method receives other types of events - not only concerns, so you need to check the type of event ( event.type == UIEventTypeTouches ). In addition, since you get a set of touches, you can check which of them have just begun, which of them have ended, moved, etc. To do this, you need to iterate through allTouches and check the phase property for each 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 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 a subclass of UIWindow

 @interface TouchWindow : UIWindow @end 

And you probably have to change this class in the .xib file in Xcode

+4
source share

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

 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:

+3
source share

UIWindow is a subclass of UIResponder that has an API for handling touch events (e.g. touchesBegan:withEvent: . Then you can subclass UIWindow, override the touch event handling API, and manage touch events yourself.

0
source share

All Articles