IOS Event Handling

From: http://blog.csdn.net/zhouxinfang/article/details/17298927    

   当一个用户发起的event发生时,UIKit创建一个event对象,该对象包含了需要处理该event的信息。然后UIKit会把该event对象添加到event队列.一个event在被分发到能够处理它的对象之前经过了很长的一段行程,首先UIApplication 会从顶层的event队列中取出一个event,然后发送给key window对象,key window对象的在这里起的作用就是根据该event的信息,找到能够处理它的最原始对象

在这里Key window通过两种方式找到能处理event的最原始的对象

Touch events:Key window会尝试deliver这些eventtouch所产生的view上面,最终找到的viewhit-test view。这种找到hit-text view的方法叫做hit-testing

Motion and remote control events:对于这些事件,Key window会将这些事件发送给first responder对象去处理。

这两种处理event的方法的最终目标是找到一个对象能够处理所产生的event。所以,UIKit会自动根据event类型发送到合适的对象去处理该event,对于touch event,这个对象就是hit-test view,对于其他的event类型,这个对象就是first responder。

Hit-Testing Returns the view where a touch occurred

IOS 使用hit-testing方法找到touch产生的viewHit-testing会检查一个touch是否发生在相关的view当中,如果是的话,会遍历该viewsubviews,在该view层次结构中最底层并且包含该touch pointview,就是所要查找的hit-test view,当检查出hit-test view后,ios就会把event传递给该view处理。

举个例子:如下图,ios根据如下顺序查找subviews

1.The touch is within the bounds of view A,so it checks subview B and C

2.The touch is not within the bounds of view B,but it is within the bounds of view C,so it checks subviews D and E.

3.The touch is not within the bounds of view D,but it is within the bounds of view E,view E is the lowest view in the view hierarchy that contains the touch,so it becomes the hit-test view.

hitTest:WithEvent;方法解析

hitTest:WithEvent方法返回hit test view,参数为CGPoint UIEvent,hitTest:WithEvent方法第一步会调用 pointInside:withEvent:方法,如果CGPoint 是在该viewbounds中时,pointInside:withEvent:返回YES,然后,该方法会在所有的子视图上递归调用hitTest:withEvent:并返回YES

如果该CGPoint不在该viewbounds中时,第一次调用pointInside:withEvent:返回NO,然后该CGPoint被忽略了,并且hitTest:WithEvent返回nil,如果有一个subview返回NO,那么整个view层次结构中的每个分支都被忽略了,因为一个touch如果不是产生在subview中,那么该subview的子subview也不可能产生。也就是说任何一个subview中的point如果是在一个superview的外面,那么该subview无法获取touch事件。当把一个subviewclipsToBounds 属性设为No时会发生这种情况。

touch event个人总结:

1.touch事件发生,创建UIEvent对象

2.按照Applicationview层次结构,逐层调用每个viewhitTest:withEvent:方法,并传入该event对象,view根据pointInside:withEvent:方法和来决定touch点是否包含在自己的bounds中;

3.如果viewbounds包含了touch点,该view会遍历自己的subview,并调用每个subviewpointInside:withEvent:方法来进一步决定touch事件是发生在自己本身,还是自己的subview上。

4.重复第二,三步,并筛选出最终接收touch事件的view对象

The Responder Chain Is Made Up of Responder Objects

许多类型的事件依赖responder chain(响应链)来传递,所谓的responder chain就是一系列的responder 对象,响应链始于first responder 结束于application对象,如果first responder不能处理一个eventios会将事件传递给响应链中的next responder

一个responder对象指能响应和处理events的对象,UIResponder 类是所有responder对象的基类,它定义的编程的接口不仅仅是为了处理event,也是为了处理常见的响应行为。UIViewController UIView UIApplication的实例都是responder对象。

first responder设计的目的是作为事件接受的第一个对象,first responder 是一个view 对象。一个对象要成为firest responder要有2个步骤。

1.重写 canBecomeFirstResponder 方法,并返回YES

2.得到 becomeFirstResponder消息,如果需要,一个对象可以给自己发送该消息

Events are not the only objects that rely on the responder chain. The responder chain is used in all of the following

Touch events:如果hit-test view不能处理touch 事件,那么事件通过响应链传递,且始于hit-test view

Text editing:当用户点击一个text field或者一个text view时,该view自动成为first responder。默认情况下虚拟键盘同时会出现,并且text field 或者text view成为编辑的焦点。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值