如何监听app所有事件

1, 修改main方法

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, NSStringFromClass([AppDelegate class]), NSStringFromClass([AppDelegate class]));
    }
}

2, 让   AppDelegate继承自UIApplication,原来是继承自UIResponder

@interface AppDelegate : UIApplication <UIApplicationDelegate>

3, 在AppDelegate中实现   sendEvent方法

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    
    NSSet *allTouches = [event allTouches];
    if (allTouches.count > 0) {
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan) {
            NSLog(@"TouchPhaseBegan");
        }
    }
}

 

 

posted on 2019-03-25 19:28  大圣ios博客  阅读(1472)  评论(0编辑  收藏  举报