iOS-事件处理(触摸事件)

2016-10-25  本文已影响0人  叫我Dragon

事件

移动应用中常用的事件
 1. 触摸事件。
 2. 加速计事件。例如:手机摇一摇
 3. 远程控制事件。例如:耳机控制
常用事件.png
处理事件的条件:
处理方法:
UIResponder内部提供的事件处理方法,可以在类中重写下列方法来处理事件:
// 触摸事件
  - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
  - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
  - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
  - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

// 加速计事件
  - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
  - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
  - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

//远程控制事件

  - (void)remoteControlReceivedWithEvent:(UIEvent *)event;

触摸事件

UIView的触摸事件处理

UIView是UIResponder的子类,通过覆盖下列四个方法处理触摸事件
覆盖下列方法后,系统会自动调用来进行事件的处理

 一根或者多根手指开始触摸View:                     
  - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

 一根或者多根手指在View移动,随着手指的移动,系统会不断调用此方法:    
  - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;   
            
一根或者多根手指离开View:
   - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

  触摸结束之前,某个系统事件(例如电话呼入)打断触摸过程
   - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

UITouch

UITouch的作用:
UITouch的常用属性
  //触摸产生时所处的窗口
  @property(nullable,nonatomic,readonly,strong) UIWindow *window;

  //触摸产生时所处的视图
  @property(nullable,nonatomic,readonly,strong) UIView *view;

  //记录了触摸事件产生时或变化时的时间,单位时秒
  @property(nonatomic,readonly) NSTimeInterval timestamp;

  //当前触摸事件所处的状态
  @property(nonatomic,readonly) UITouchPhase phase;

  //短时间内点击屏幕的次数,可以根据此属性判断单击,双击,或更多点击
  @property(nonatomic,readonly) NSUInteger tapCount;

  //iOS9开始支持的属性,触摸类型,包括直接触摸,间接触摸,触控笔触摸
  @property(nonatomic,readonly) UITouchType type;
  typedef NS_ENUM(NSInteger, UITouchType) {
      UITouchTypeDirect, //直接触摸
      UITouchTypeIndirect, //间接触摸
      UITouchTypeStylus, //触控笔触摸
  };

  @property(nonatomic,readonly) CGFloat majorRadius //触摸的半径;
UITouch的常用方法
 //返回触摸点在view上的位置,针对view的坐标系统(以view的左上角为原点),传入的view为空时,返回的触摸点在UIWindow的位置
 - (CGPoint)locationInView:(nullable UIView *)view;

 //返回前一个触摸点的位置
 - (CGPoint)previousLocationInView:(nullable UIView *)view;

 //精确的触摸点位置 iOS9.1之后
 - (CGPoint)preciseLocationInView:(nullable UIView *)view   NS_AVAILABLE_IOS(9_1);

 //精确的前一个触摸点的位置 iOS9.1之后
 - (CGPoint)precisePreviousLocationInView:(nullable UIView *)view NS_AVAILABLE_IOS(9_1);

UIEvent

UIEvent常用属性
 //事件类型
 @property(nonatomic,readonly) UIEventType     type NS_AVAILABLE_IOS(3_0);
 @property(nonatomic,readonly) UIEventSubtype  subtype NS_AVAILABLE_IOS(3_0);

 //事件产生的时间
 @property(nonatomic,readonly) NSTimeInterval  timestamp;
UIEvent常用方法
 //获得窗口上的UITouch对象
 - (nullable NSSet <UITouch *> *)touchesForWindow:(UIWindow *)window;

 //获得view上的UITouch对象
 - (nullable NSSet <UITouch *> *)touchesForView:(UIView *)view;

touches和event参数

一次完整的触摸过程,会经历3个状态:

 触摸开始:                     
  - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

 触摸移动:   
  - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;   
            
 触摸结束:
  - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

 触摸消失(可能会经历):
  - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

事件的产生和传递(触摸事件)

1.触摸事件发生后,系统会将该事件加入到一个由UIApplication管理的事件队列中。
2.UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发送事件给应用程序的主窗口(keyWindow)。
3.主窗口会在视图层次结构中,找到一个最合适的视图来处理事件,这也是整个事件处理过程中的第一步。
4.找到合适的视图后,就会调用视图控件的touches方法来做具体的事件处理

即: 触摸(事件发生) ——> UIApplication管理的事件队列 ——> 主窗口(keyWindow) ——> 合适的视图 ——> 视图通过方法来处理事件


事件传递.png

响应者链条

响应者链条.png
响应者链条的事件传递过程:

手势识别器

iOS3.2之后,苹果推出手势识别功能(Gesture Recognizer)。

    UITapGestureRecognizer(敲击)
    UIPinchGestureRecognizer(捏合,用于缩放)
    UIPanGestureRecognizer(拖拽)
    UISwipeGestureRecognizer(轻扫)
    UIRotationGestureRecognizer(旋转)
    UILongGestureRecognizer(长按)
手势识别器的使用:
    //1.创建敲击手势识别器
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] init];
    
    //2.给要进行处理触摸事件的view添加手势识别,这里给self.view添加手势识别
    [self.view addGestureRecognizer:tapGR];
    
    //3.添加监听方法
    tapGR addTarget:self action:@selector(tapMethod:) //实现tapMethod:方法来处理具体的操作
上一篇 下一篇

猜你喜欢

热点阅读