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;
}
然后可以在其它地方创建按钮,就可以截获到按钮触摸事件!
<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;
}
然后可以在其它地方创建按钮,就可以截获到按钮触摸事件!
相关文章推荐
- 通过消息拦截达到在Delphi控件中屏蔽原有事件处理的一种方法
- 【iOS开发-88】事件传递原理解释哪个控件处理事件以及响应者链条的介绍
- 【iOS开发-88】事件传递原理解释哪个控件处理事件以及响应者链条的介绍
- iOS中的事件处理流程
- CListCtrl控件主要事件及LVN_ITEMCHANGED消息和鼠标双击列表项事件的处理;属性类型设定
- CListCtrl控件主要事件及LVN_ITEMCHANGED消息和鼠标双击列表项事件的处理
- IOS的基本控件的使用-UIButton的事件处理
- IOS 代码创建控件,并有处理事件
- 基本上每个消息都有一个Result,代表是否(正确)处理过了(但是是否继续传递,还得研究)
- Android事件处理之四 Handler消息传递机制
- iOS事件拦截处理
- [置顶] Android开发知识(七):Android事件处理机制:事件分发、传递、拦截、处理机制的原理分析(上)
- [置顶] Android开发知识(八):Android事件处理机制:事件分发、传递、拦截、处理机制的原理分析(中)
- iOS开发中的事件处理(二)-事件传递,响应者链条-
- iOS事件拦截处理
- ios开发事件处理之 :二:事件的产生与传递
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。
- CListCtrl控件主要事件及LVN_ITEMCHANGED消息和鼠标双击列表项事件的处理
- iOS开发-Day28-UI自定义控件&消息传递&事件传递机制
- iOS:事件处理机制(二)--事件传递,Responder Chain响应链