如何监听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 

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");
        }
    }
}

 

 

转载于:https://www.cnblogs.com/dashengios/p/10595775.html

你可能感兴趣的:(如何监听app所有事件)