事件的产生、传递

iOS事件的传递和响应(iOS响应者链条)

2015-09-01  本文已影响458人  YotrolZ

一 . 响应者对象

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;
-(void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
-(void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
-(void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
-(void)remoteControlReceivedWithEvent:(UIEvent*)event;
UITouch
-(CGPoint)locationInView:(UIView*)view;
返回值表示触摸在view上的位置
这里返回的位置是针对view的坐标系的(以view的左上角为原点(0,0))
调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
 - (CGPoint)previousLocationInView:(UIView*)view;
该方法记录了前一个触摸点的位置
UIEvent

二 . 事件的产生和传递(重点)

三 . 事件的响应(重点)

响应者链条.png

总结:

事件传递的完整过程:
简而言之:

1.事件的传递 : 由UIApplication --> UIWindow --> 递归找到最合适的响应者

2.事件的响应 : 调用最合适的响应者的touches方法--> 如果其没有实现,默认做法,将事件传递给上一个响应者-->找到上一个响应者,调用它的touches方法

上一篇 下一篇

猜你喜欢

热点阅读