How to respond to touch events in UIWindow?

前端 未结 4 1015
不思量自难忘°
不思量自难忘° 2020-12-30 14:11

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条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 14:49

    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.

提交回复
热议问题