理解Mac和iOS中的 Event 处理

 根据现在的理解,我把event处理分为5部分,第一是,Event处理的Architecture;第二是,Event的Dispatch到first responder之前; 第三是,Event从first responder出来后;第四是,Action与Event的关系;第五是,Apple Event的处理。

1. Event Handling Architecture

下面这张图解释了在MacOSX上event处理的过程。

 The event stream

Before it dispatches an event to an application, the window server processes it in various ways; it time-stamps it, annotates it with the associated window and process port, and possibly performs other tasks as well. As an example, consider what happens when a user presses a key. The device driver translates the raw scan code into a virtual key code which it then passes off (along with other information about the key-press) to the window server in an event record. The window server has a translation facility that converts the virtual key code into a Unicode character.

In OS X, events are delivered as an asynchronous stream. This event stream proceeds “upward” (in an architectural sense) through the various levels of the system—the hardware to the window server to the Event Manager—until each event reaches its final destination: an application. As it passes through each subsystem, an event may change structure but it still identifies a specific user action.

在event到达Window Server之后,就等着发给各个Application去处理了,如果一个App是基于Cocoa的,它会使用Run Loop从Window Server中获取应该由本App处理的event。

The main event loop, with event source

从这个图可以看出,每个应用有一个Event Source与之对应。Event Source中有个Mach port用于从Window Server接收Event,然后存放在一个队列中。每个应用的Main run Loop是一个只负责从event source中取 event,如果没有event,Run Loop就会Block住,一直等到有event为止。之所以这里有个Main run loop是因为,在Cocoa应用中,每个线程都有一个run loop,主线程的run loop叫main run loop。Run loop就是一个NSRunLoop对象。Apple event一会通过Run Loop进入一个App,详细可以见Apple Event部分的描述。

 

 

2. Event Dispatch : Arriving at First Responder;

 

3. Event Dispatch: After First Responder;

 

4. Action 

 

 

 

参考文档

1. Cocoa Event Handling Guide: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW2

2. Event Handling Guide for iOS:https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html#//apple_ref/doc/uid/TP40009541-CH4-SW2

3. Mac vs. iOS event 比较:https://developer.apple.com/library/ios/documentation/general/conceptual/Devpedia-CocoaApp/Responder.html

 

转载于:https://www.cnblogs.com/whyandinside/p/3405769.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值