iOS拦截控件事件,处理后继续执行原来的消息传递流程

来源:互联网 发布:linux 7 配置ip地址 编辑:程序博客网 时间:2024/04/29 23:36


#import <objc/objc.h>

#import <objc/runtime.h>



- (void)sendEventHooked:(UIEvent *)event {

    

    

    //在这里做你想做的事情吧

   NSLog(@"截获事件:%@", [eventdescription]);

    

    

    

    //执行原来的消息传递流程

    

    [selfperformSelector:@selector(sendEventOriginal:)withObject:event];

    

}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{


    Method sendEvent =class_getInstanceMethod([UIWindowclass],@selector(sendEvent:));

   Method sendEventMySelf =class_getInstanceMethod([selfclass],@selector(sendEventHooked:));

   IMP sendEventImp =method_getImplementation(sendEvent);

   class_addMethod([UIWindowclass],@selector(sendEventOriginal:), sendEventImp,method_getTypeEncoding(sendEvent));

   IMP sendEventMySelfImp =method_getImplementation(sendEventMySelf);

   class_replaceMethod([UIWindowclass],@selector(sendEvent:), sendEventMySelfImp,method_getTypeEncoding(sendEvent));


    

self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];

    // Override point for customization after application launch.

    if ([[UIDevicecurrentDevice]userInterfaceIdiom] ==UIUserInterfaceIdiomPhone) {

        self.viewController = [[[ViewControlleralloc]initWithNibName:@"ViewController_iPhone"bundle:nil]autorelease];

    }else {

        self.viewController = [[[ViewControlleralloc]initWithNibName:@"ViewController_iPad"bundle:nil]autorelease];

    }

    self.window.rootViewController =self.viewController;

    [self.windowmakeKeyAndVisible];


return YES;

}




然后可以在其它地方创建按钮,就可以截获到按钮触摸事件!



原创粉丝点击