如何监听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");
        }
    }
}

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM