iOS程序之事件处理流程

来源:互联网 发布:linux w3m 使用 编辑:程序博客网 时间:2024/05/29 03:29

在iOS系统中有个很重要的概念:Responder。基本上所有的UI相关的控件,view和viewcontroller都是继承自UIResponder。事件的分发正是通过由控件树所构成的responderchain(响应链)所进行的。一个典型的iOS响应链如下:

当用户发起一个事件,比如触摸屏幕或者晃动设备,系统产生一个事件,同时投递给UIApplication,而UIApplication则将这个事件传递给特定的UIWindow进行处理(正常情况都一个程序都只有一个UIWindow),然后由UIWindow将这个事件传递给特定的对象(即firstresponder)并通过响应链进行处理。虽然都是通过响应链对事件进行处理,但是触摸事件和运动事件在处理上有着明显的不同(主要体现在确定哪个对象才是他们的firstresponder):
看起来很对路,触摸事件发生后,会先经过hitTest确定触摸事件发生在哪个view上,然后该事件会经由sendEvent分发到“合适”的对象进行处理,也就是说sendEvent相当于事件的中转站,在这里可以拦截所有的iOS事件。
在iOS系统中,一共有三种形式的事件:触摸事件(TouchEvent),运动事件(MotionEvent)和远端控制事件(Remote-controlEvent)。顾名思义,触摸事件就是当用户触摸屏幕时发生的事件,而运动事件是用户移动设备时发生的事件:加速计,重力感应。远端控制事件可能比较陌生:如通过耳机进行控制iOS设备声音等都属于远端控制事件—-下面不展开说,因为和主题无关,详细的内容可以参考: 《RemoteControl of Multimedia》 。

具体流程是这样:
1. 新建一个自定义的UIApplication(MyApplication),并替换系统默认的UIApplication:
在程序入口处(main.m)修改代码,这样程序就会调用我们的自定义Application类

int main(int argc, char *argv[]){  @autoreleasepool  {    returnUIApplicationMain(argc, argv, NSStringFromClass([MyApplicationclass]), NSStringFromClass([AppDelegate class]));  }}

2. 在MyApplication中实现sendEvent函数,利用系统通知中心(NSNotificationCenter)发送触摸事件:

-(void)sendEvent:(UIEvent*)event{  if(event.type==UIEventTypeTouches)  {    if([[event.allTouches anyObject] phase]==UITouchPhaseBegan)    {      //响应触摸事件(手指刚刚放上屏幕)      [[NSNotificationCenterdefaultCenter] postNotification:[NSNotificationnotificationWithName:nScreenTouch object:nil userInfo:[NSDictionarydictionaryWithObject:event forKey:@"data"]]];      //发送一个名为‘nScreenTouch’(自定义)的事件    }  }  [supersendEvent:event];}

3. 在selectorView的构造函数中注册nScreenTouch事件,并判断该次触摸时间是否由selectorView引发,如果不是,则隐藏selectorView。

-(id)initWithFrame:(CGRect)frame{  self= [super initWithFrame:frame];  if(self)  {    //注册nScreenTouch事件    [[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(onScreenTouch:)name:nScreenTouch object:nil];  }  return self;}-(void)onScreenTouch:(NSNotification *)notification{  UIEvent*event=[notification.userInfo objectForKey:@"data"];  UITouch*touch=[event.allTouches anyObject];  if(touch.view!=self)  {    //取到该次touch事件的view,如果不是触摸了selectorView,则隐藏selectorView.    [UIViewanimateWithDuration:0.5 animations:^    {      self.alpha=0;    }];    [UIViewcommitAnimations];  }}
远端控制事件(Remote-control Event)。顾名思义,触摸事件就是当用户触摸屏幕时发生的事件,而运动事件是用户移动设备时发生的事件:加速计,重力感应。远端控制事件可能比较陌生:如通过耳机进行控制iOS设备声音等都属于远端控制事件—-下面不展开说