iphone--截取事件分发

来源:互联网 发布:win7数据库在哪 编辑:程序博客网 时间:2024/06/05 20:48

有时当我们想在应用点击时能知道点击事件,不管用户点击的是按钮还是文本框,我们都能在点击时做一些操作,或例如在游戏中,设置用户点击效果的样式,或播放一个声音。当有这种需求时,可以用下面我的这种方法:

我们不是去监听每一个控件事件,我们只要UIApplication UIWindow 并重写(overridesendEvent: 方法来监控事件,如果你重写这些方法,请确认调用超类的实现(也就是,[super sendEvent:theEvent]),不然结果其他的视图不会接收到事件;

- (void)sendEvent:(UIEvent *)event{    NSLog(@"Event:%d",index++);    //做你的操作。。。。。    [super sendEvent:event];}

如果我们子类化UIWindow后,在Appdelegate中将UIWindow改成你所定义的子类就行了;

如果你子类化UIApplication后,就要在工程入口类 mail.m文件上修改一下,如下

#import <UIKit/UIKit.h>#import "AppDelegate.h"#import "FEUIEventApplication.h"int main(int argc, char *argv[]){    @autoreleasepool {        //[FEUIEventApplication class] 你自定义的子类        return UIApplicationMain(argc, argv, NSStringFromClass([FEUIEventApplication class]), NSStringFromClass([AppDelegate class]));    }}

参考文章:http://blog.csdn.net/iefreer/article/details/4754482

关于完整的ios事件分发处理机制,可以看下中文文档:http://download.csdn.net/detail/cjsen/4852469