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就称为