您可以覆盖-[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()
:使用此机制这只适用于UIControl子类,其中发送他们的行动对他们的目标
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyApplicationSubclass class]), NSStringFromClass([AppDelegate class]));
}
}
注意。如果您想查看所有通过应用的触摸事件,请改为使用-[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
您需要响应水龙头的方法。他们每个人都可以确定是什么导致了自来水。 (如果你需要知道事件处理结束后,你可以创建一些'id lastTapTarget'变量并保存它。) –
根据文档,它说事件对象首先被发送到UIApplication,然后到窗口,然后到视图。在到达那里时是否有办法拦截这些事件?我不想开始在一个应用程序中为一堆对象编写自来水方法,我不拥有 –
您如何看待这个问题?有几种方法可以回答你的问题。对于UIControl子类,您可以始终定义在进行特定触摸时调用的操作。如果你只是按照触摸事件,那么你可能需要得到沙发的坐标,并确定哪个UIView当前占用的空间。有更多的选择。真的,这取决于。请为您的情况提供更多背景。 –