你可以覆盖-[UIApplication sendAction:to:from:forEvent]
做你想做的事:
@implementation MyApplicationSubclass
- (BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
{
NSLog(@"Sending action %@ from sender %@ to target %@ for event %@", NSStringFromSelector(action), sender, target, event);
return [super sendAction:action to:target from:sender forEvent:event];
}
@end
把它放在 UIApplication 的自定义子类中。然后,在 main.m 中,将调用更改为UIApplicationMain()
以便使用您的自定义子类:
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplicationSubclass class]), NSStringFromClass([AppDelegate class]));
}
}
请注意,这仅适用于 UIControl 子类,它们使用此机制将其操作发送到其目标。如果您想查看通过应用程序的所有触摸事件,请-[UIApplication sendEvent:]
改为覆盖。在这种情况下,由您决定哪个对象将接收触摸。您可以通过调用-hitTest:
主视图/窗口来做到这一点,但请记住,这会确定触摸落在哪个视图上,而不一定是哪个视图处理它(例如,视图可以将事件转发给其他对象)。像这样的东西:
@implementation MyApplicationSubclass
- (void)sendEvent:(UIEvent *)event
{
UIWindow *window = [self keyWindow];
NSSet *touches = [event touchesForWindow:window];
for (UITouch *touch in touches) {
UIView *touchedView = [window hitTest:[touch locationInView:window] withEvent:event];
NSLog(@"Touch %@ received in view %@ for event %@", touch, touchedView, event);
}
[super sendEvent:event];
}
@end