3.1 iOS事件概述

作者: 刘2傻 | 来源:发表于2016-12-31 12:31 被阅读56次

1.1事件概述

(一)什么是事件?
事件就是一个包含了用户操作信息并会被发送给应用程序的对象.

iOS系统中事件分三种:触摸事件,加速计事件,远程控制事件

14635482186356.png

(二)怎么处理这些事件?
在iOS系统中不是任何对象都能够处理这些事件,只有继承了UIResponder的对象才能够处理这些事件.我们把继承了UIResponder的对象成为"响应者对象"

UIApplication,UIViewController,UIView都继承了UIResponder,所以他们都是响应者对象,可以接收和处理这些事件.

(三)UIResponde类常用方法

// 返回下一个响应者
- (UIResponder*)nextResponder;
// 判断是否可以成为第一响应者(默认返回NO)
- (BOOL)canBecomeFirstResponder;    // default is NO
// 成为第一响应者
- (BOOL)becomeFirstResponder;
// 判断是否能够失去第一响应者(默认返回YES)
- (BOOL)canResignFirstResponder;    // default is YES
// 失去第一响应者
- (BOOL)resignFirstResponder;
// 判断是不是第一响应者
- (BOOL)isFirstResponder;


/*触摸事件相关*/
// 当手指开始触摸屏幕时
- (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

(四)UITouch 类介绍

每一个UITouch类的对象代表当前的一个手指,当用户用一根手指触摸屏幕时系统都会创建一个UITouch对象与之关联.
作用:用来保存手指在屏幕上面触摸相关的信息,如:触摸的位置,时间,阶段等.
当手指移动时系统会更新UITouch对象,以保存手指的触摸位置,当手指离开屏幕时系统就会销毁对应手指关联的UITouch对象.

UITouch类常用属性和方法

/*常用属性*/
// 触摸时的时间变化(单位:秒)
@property(nonatomic,readonly) NSTimeInterval      timestamp;
// 当前触摸事件的状态
@property(nonatomic,readonly) UITouchPhase        phase;
// 短时间内点按屏幕的次数
@property(nonatomic,readonly) NSUInteger          tapCount; 
// 触摸类型 
@property(nonatomic,readonly) UITouchType         type
// 触摸事件所对应的窗口
@property(,nonatomic,readonly,strong) UIWindow                        *window;
// 触摸事件作用的view
@property(,nonatomic,readonly,strong) UIView                          *view;
// 手势识别的集合
@property(,nonatomic,readonly,copy)   NSArray <UIGestureRecognizer *> *gestureRecognizers
/*常用方法*/

/*
1.返回触摸在指定view上面的位置.
2.该位置参考的是给定view的坐标系(以指定view的左上角为圆点(0,0)).
3.当指定view为nil时,返回的位置为在UIWindow的位置.
*/
- (CGPoint)locationInView:(UIView *)view;
// 返回前一个触摸点在制定view上的位置
- (CGPoint)previousLocationInView:(UIView *)view;

(五)UIEvent介绍

一个UIEvent对象就代表系统的一个事件.系统每产生一个事件,就会创建一个UIEvent对象.UIEvent提供了一些方法可以获取当前事件对应view的触摸对象(UITouch).
作用:用来保存事件产生的时间和类型.

常见属性和方法

/*常用属性*/
// 事件类型
@property(nonatomic,readonly) UIEventType     type ;
@property(nonatomic,readonly) UIEventSubtype  subtype ;
// 事件产生的时间
@property(nonatomic,readonly) NSTimeInterval  timestamp;

/*常用方法*/
// 返回该事件的所有UITouch对象
- (NSSet <UITouch *> *)allTouches;
// 返回指定窗口的所有UITouch对象
- (NSSet <UITouch *> *)touchesForWindow:(UIWindow *)window;
// 返回指定view的所有UITouch对象
- (NSSet <UITouch *> *)touchesForView:(UIView *)view;

(六)UIView的触摸事件

UIView是UIResponder的子类,所以可以实现下面4个方法监听UIView的触摸事件

  • (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;
    一次完整的触摸事件会经历3个状态:
    触摸开始,触摸移动,触摸结束.(触摸取消(可能会经历))分别对应touchesBegan,touchesMoved,touchesEnded和touchesCancelled方法.
    我们可以重写UIView的这些方法来监听触摸的不同状态

注意事项!!!

1.一次完整的触摸过程中,只会产生一个(UIEvent)事件对象,4个触摸方法都是同一个事件对象.
2.如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches里面装着2个UITouch对象
3.如果两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象.
4.可以根据touches参数中UITouch对象的个数,判断是点触摸还是多点触摸.

相关文章

  • 3.1 iOS事件概述

    1.1事件概述 (一)什么是事件?事件就是一个包含了用户操作信息并会被发送给应用程序的对象. iOS系统中事件分三...

  • iTop系统使用手册

    目录 CMDB概述 iTop系统概述 iTop功能操作3.1. 配置管理3.2. 变更管理3.3. 事件管理3.4...

  • iOS事件

    概述 iOS中事件有触摸事件、加速计事件、远程控制事件,下面以触摸事件为例研究下iOS事件相关的内容 UIResp...

  • 宠物类app竞品分析(终于干了点本专业的事)

    一、概述 1、产品名称与版本(IOS) ①遛遛2.7 ②闻闻窝宠物2.5.1 ③乐宠3.1 ④有宠2.1.1 ⑤握...

  • 响应与控制 --- UIResponder及相关类

    一、概述: 在iOS中事件一般分为三类在iOS 9后又添加了关于深按的事件: 触摸事件:通过触摸或者手势进行触发(...

  • 事件传递和响应机制

    iOS 事件传递和响应机制 一.概述 APPs通过responder objects来接收和处理点击事件,resp...

  • 3.1 页面概述

    页面模块 泛指系统与人交互的所有界面,包括输入的界面和展示出来的效果。管理系统开发的主要工作量都在页面上,包括逻辑...

  • word

    word序号与文字空格大 3->3.1(3.1->3.2同理)需要将"需求概述"设置为3.1

  • iOS中事件传递的那点事情

    一> 基础概述 1> ios当中常用的事件分为三种: 触摸事件 加速计事件 远程控制事件 2> 什么是响应者对象:...

  • 笔记整理:响应者链 和 第一响应者

    一、概述 iOS中能响应事件的都必须继承于UIResponder类。例如UIApplication 、UIWind...

网友评论

    本文标题:3.1 iOS事件概述

    本文链接:https://www.haomeiwen.com/subject/fvivvttx.html