在ios开发中,UIKit可识别三种输入事件:UIEventTypeTouches(触摸事件),UIEeventTypeMotion(运动事件),UIEventRemoteContro(远程控制事件)。UIEvent对象事件抽象的类。
在接受触摸事件之前,先说明一下几个概念:
响应者对象:只有继承了UIResponder的对象才能接受并处理事件。
常见的响应者对象:UIApplication,UIviewcontroller ,UIview 等以及继承UIview的控件。
查看UIResponder类可以发现:有4个方法处理触摸不同阶段的事件:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
- (void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
但是,通常会使用UITouch对象而不是UIEvent对象来处理触摸事件;UITouch对象还会保存一些其他信息,比如,手指的前一个位置、手指按下屏幕的次数(tapCount, 可以用来判断单击和双击事件)
- (CGPoint)locationInView:(UIView *)view:
返回一个CGPoint类型的值,表示触摸在view这个视图上的位置,这里返回的位置是针对view的坐标系的。
- (CGPoint)previousLocationInView:(UIView *)view:
该方法记录了前一个坐标值,函数返回也是一个CGPoint类型的值,