您的位置:首页 > 移动开发 > IOS开发

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

2013-04-02 12:17 441 查看
#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;
}

然后可以在其它地方创建按钮,就可以截获到按钮触摸事件!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐