iOS学习9_事件分发&响应链

iOS的三种事件:触摸事件/运动事件/远程控制事件

typedef enum {
UIEventTypeTouches,
UIEventTypeMotion,
UIEventTypeRemoteControl,
} UIEventType;

只有继承UIResponder类的对象才能处理事件,如UIView、UIViewController、UIApplication都继承自UIResponder,都能接收并处理事件。UIResponder中定义了上面三类事件相关的处理方法:

下面主要讨论触摸事件。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;   
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;  
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;   
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;


事件分发:Hit-Testing View

当用户触摸了设备的屏幕,iOS会识别这一系列触摸事件并且封装到UIEvent对象里面,并且放到application的事件队列,在处理事件时,UIApplication会从事件队列取出最前面的事件并分发处理,一般他会发送给主窗口,接下来主窗口将该事件传递到an initial object来处理,对于触摸事件来说该对象是Touch events


首先会将该事件发送给最合适的对象来处理。对于触摸事件该对象指的是hit-test view,对于其他事件,该对象指的是第一响应者。

UIKit first sends the event to the object that is best suited to handle the event. For touch events, that object is the hit-test view, and for other events, that object is the first responder.

Hit-Testing 指的是找到当前触摸事件在哪个View上发生的过程:如果触摸在一个View的边界里面,就会递归的检查它的子View是不是包含了触摸点。最外面的包含了该触摸点的View就称为

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值