touchesBegan 触摸事件

一、概念介绍

1、在用户使用App过程中,会产生各种各样的事件,iOS中的事件可以分为3大类型:

1)触摸事件

2)加速计事件

3)远程操控事件

  • 响应者对象UIResponder

        在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”。

        UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能接收并处理事件。

二、UIReponder类

UIResponder内部提供了一下方法来处理事件,父类是NSObject

1、触摸事件

1)手指按下事件

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;

2)手指移动事件

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;

3)手指抬起事件

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;

4)意外中断事件(如打电话打扰)

- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;

5)3D触摸事件

- (void)touchesEstimatedPropertiesUpdated:(NSSet * _Nonnull)touches

2.加速计事件(一般用于可以产生加速计事件的设备,如微信的摇一摇功能)

1)开始加速

- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;

2)结束加速

- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;

3)加速中断

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;

3.远程控制事件(一般用于遥控)

1)开始按压的时候的调用

- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event

2)按压改变的时候调用

- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event

3)按压结束的时候调用

- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event

4)当系统发出取消按压事件的时候调用

- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event

三、UITouch类

UITouch父类是NSObject,存放在NSSet中,无序且不能重复,通过anyObject来访问单个元素。,通过forin循环来遍历NSSet中的每一个元素,当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象, 保存着跟本次手指触摸相关的信息,比如触摸的位置、时间、阶段, 当手指离开屏幕时,系统会销毁相应的UITouch对象。

1、属性:

1)获取触摸产生时所处的窗口

@property(nonatomic,readonly,retain) UIWindow  *window;

2)获取触摸产生时所处的视图

@property(nonatomic,readonly,retain) UIView  *view;
3)获取短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击
@property(nonatomic,readonly) NSUInteger tapCount;

4)获取触摸事件产生或变化时的时间,单位是秒

@property(nonatomic,readonly) NSTimeInterval timestamp;

5)获取当前触摸事件所处的状态

触摸事件在屏幕上有一个周期,即触摸开始、触摸点移动、触摸结束,还有中途取消。而通过phase可以查看当前触摸事件在一个周期中所处的状态

@property(nonatomic,readonly) UITouchPhase  phase;
UITouchPhase 枚举:
UITouchPhaseBegan 开始触摸
UITouchPhaseMoved 移动
UITouchPhaseStationary 停留
UITouchPhaseEnded 触摸结束
UITouchPhaseCancelled 触摸中断

6)触摸类型

@property(nonatomic,readonly) UITouchType type;
UITouchType 枚举:
UITouchTypeDirect 垂直的触摸类型
UITouchTypeIndirect 非垂直的触摸类型
UITouchTypeStylus 水平的触摸类型

7)获取手指与屏幕的接触半径

@property(nonatomic,readonly) CGFloat majorRadius;

8)获取触摸手势

@property(nullable,nonatomic,readonly,copy)   NSArray <UIGestureRecognizer *> *gestureRecognizers;

9)获取触摸压力值,一般压力值为1.0

@property(nonatomic,readonly) CGFloat force;

10)获取最大触摸压力值

@property(nonatomic,readonly) CGFloat maximumPossibleForce;

2、方法

1)返回当前触摸点在view上的位置,这里返回的位置是针对view的坐标系(以view的左上角为圆点(0,0)),调用时传入view参数为nil的话,返回的是触摸点在UIWindow的位置

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

2)返回前一个触摸点的位置

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

3)当前触摸对象的坐标

- (CGPoint)preciseLocationInView:(nullable UIView *)view;

4)当前触摸对象的前置坐标

- (CGPoint)precisePreviousLocationInView:(nullable UIView *)view;

四、UIEvent类

UIEvent:成为事件对象,记录产生的时刻和类型,事件对象中包含于当前多点触摸序列相对应的所有触摸对象,还可以提供与特定视图或窗口相关联的触摸对象。

1、属性

1)获取事件类型

@property(nonatomic,readonly) UIEventType type;
UIEventType枚举:
UIEventTypeTouches 触摸事件
UIEventTypeMotion 加速事件
UIEventTypeRemoteControl 远程控制事件
UIEventTypePresses 按压事件

2)获取远程控制事件

@property(nonatomic,readonly) UIEventSubtype  subtype;
UIEventSubtype 枚举:
// 不包含任何子事件类型
UIEventSubtypeNone                              = 0,
// 摇晃事件(从iOS3.0开始支持此事件)
UIEventSubtypeMotionShake                       = 1,
//远程控制子事件类型(从iOS4.0开始支持远程控制事件)
//播放事件【操作:停止状态下,按耳机线控中间按钮一下】
UIEventSubtypeRemoteControlPlay                 = 100,
//暂停事件
UIEventSubtypeRemoteControlPause                = 101,
//停止事件
UIEventSubtypeRemoteControlStop                 = 102,
//播放或暂停切换【操作:播放或暂停状态下,按耳机线控中间按钮一下】
UIEventSubtypeRemoteControlTogglePlayPause      = 103,
//下一曲【操作:按耳机线控中间按钮两下】
UIEventSubtypeRemoteControlNextTrack            = 104,
//上一曲【操作:按耳机线控中间按钮三下】
UIEventSubtypeRemoteControlPreviousTrack        = 105,
//快退开始【操作:按耳机线控中间按钮三下不要松开】
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
//快退停止【操作:按耳机线控中间按钮三下到了快退的位置松开】
UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
//快进开始【操作:按耳机线控中间按钮两下不要松开】
UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
//快进停止【操作:按耳机线控中间按钮两下到了快进的位置松开】
UIEventSubtypeRemoteControlEndSeekingForward    = 109,
(3)获取触摸产生或变化的时间戳
@property(nonatomic,readonly) NSTimeInterval  timestamp;

2、方法

1)获取触摸点的集合,可以判断多点触摸事

- (nullable NSSet <UITouch *> *)allTouches;

2)获取指定窗口里的触摸点

- (nullable NSSet <UITouch *> *)touchesForWindow:(UIWindow *)window;

3)获取指定视图里的触摸点

- (nullable NSSet <UITouch *> *)touchesForView:(UIView *)view;

4)获取手势对象

- (nullable NSSet <UITouch *> *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture;

五、实例

1、获取触摸点的坐标

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    
    //获取触摸点的集合
    NSSet * allTouches = [event allTouches];
    //获取触摸对象
    UITouch * touch = [allTouches anyObject];
    //返回触摸点所在视图中的坐标
    CGPoint point = [touch locationInView:[touch view]];
    NSLog(@"point--x:%f y:%f",point.x,point.y);
    
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值