现在的位置: 首页 > 综合 > 正文

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

2013年08月13日 ⁄ 综合 ⁄ 共 1440字 ⁄ 字号 评论关闭

#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;

}

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

抱歉!评论已关闭.