iOS开发之触摸事件

2016-04-27  本文已影响59人  搁浅的青蛙

本文介绍了iOS中使用频率较高的触摸事件,并阐述了事件产生和传递的过程,以及响应者链的事件传递过程

iOS触摸.jpg

触摸事件


简介

响应者对象

-(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;
2. 加速计事件
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
3. 远程控制事件
-(void)remoteControlReceivedWithEvent:(UIEvent *)event;

UIView的触摸事件处理

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
2. 一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
3. 一根或者多根手指离开view,系统会自动调用view的下面方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
4. 触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    - *提示:touches中存放的都是UITouch对象*

UITouch

对象和作用

UITouch的属性

@property(nonatomic,readonly,retain) UIWindow    *window;
@property(nonatomic,readonly,retain) UIView      *view;
@property(nonatomic,readonly) NSUInteger          tapCount;
@property(nonatomic,readonly) NSTimeInterval      timestamp;
@property(nonatomic,readonly) UITouchPhase        phase;

UITouch的方法

-(CGPoint)locationInView:(UIView *)view;
-(CGPoint)previousLocationInView:(UIView *)view;

UIEvent

@property(nonatomic,readonly) UIEventType     type;
@property(nonatomic,readonly) UIEventSubtype  subtype;
- 事件产生的时间
@property(nonatomic,readonly) NSTimeInterval  timestamp;

touches和event参数

-(void)touchBegan:(NSSet *)touches withEvent:(UIEvent *)event
2. 触摸移动
-(void)touchMoved:(NSSet *)touches withEvent:(UIEvent *)event
3. 触摸结束
-(void)touchEnded:(NSSet *)touches withEvent:(UIEvent *)event
4. 触摸取消
-(void)touchCancelled:(NSSet *)touches withEvent:(UIEvent *)event

事件产生和传递


响应者链条


响应者链条示意图

事件传递的完整过程

  1. 先将事件对象由上往下传递(由父控件传递给子控件),找到最合适的控件来处理这个事件。
  2. 调用最合适控件的touches….方法
  3. 如果调用了[super touches….];就会将事件顺着响应者链条往上传递,传递给上一个响应者
  4. 接着就会调用上一个响应者的touches….方法

响应者链的事件传递过程

  1. 如果view的控制器存在,就传递给控制器;如果控制器不存在,则将其传递给它的父视图
  2. 在视图层次结构的最顶级视图,如果也不能处理收到的事件或消息,则其将事件或消息传递给window对象进行处理
  3. 如果window对象也不处理,则其将事件或消息传递给UIApplication对象
  4. 如果UIApplication也不能处理该事件或消息,则将其丢弃

手势识别


为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer;
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
- 设置手势识别器对象的具体属性
// 连续敲击2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起敲击
tap.numberOfTouchesRequired = 2;
- 添加手势识别器到对应的view上
[self.iconView addGestureRecognizer:tap];
- 监听手势的触发
[tap addTarget:self action:@selector(tapIconView:)];
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    // 没有触摸事件发生,所有手势识别的默认状态
    UIGestureRecognizerStatePossible,
    // 一个手势已经开始但尚未改变或者完成时
    UIGestureRecognizerStateBegan,
    // 手势状态改变
    UIGestureRecognizerStateChanged,
    // 手势完成
    UIGestureRecognizerStateEnded,
    // 手势取消,恢复至Possible状态
    UIGestureRecognizerStateCancelled, 
    // 手势失败,恢复至Possible状态
    UIGestureRecognizerStateFailed,
    // 识别到手势识别
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};
上一篇下一篇

猜你喜欢

热点阅读