IOS中的触摸事件

1. ios中的事件概述
    在ios中会有各种各样的事件,它们可以分为3类:触摸事件、加速计事件、远程控制事件。

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

3. 对UIResponder的介绍
14个触摸事件
    - (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; // 触摸结束前:某个系统事件(电话呼入)会打断触摸过程
说明:四个触摸事件方法中都有NSSet *touches和UIEvent *event两个参数
    (1)一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数。
    (2)如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象。
    (3)如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象。
    (4)根据touches中UITouch的个数可以判断出是单点触摸还是多点触摸。
23个加速计事件
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
31个远程控制事件
    - (void)remoteControlReceivedWithEvent:(UIEvent *)event NS_AVAILABLE_IOS(4_0);

4.UITouch类详解
1)属性
@property(nonatomic,readonly) NSTimeInterval      timestamp; // 触摸事件产生或变化时的时间,单位是秒
@property(nonatomic,readonly) UITouchPhase        phase;    // 当前触摸事件所处的状态
@property(nonatomic,readonly) NSUInteger          tapCount;   // 短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击

// majorRadius and majorRadiusTolerance are in points
// The majorRadius will be accurate +/- the majorRadiusTolerance
@property(nonatomic,readonly) CGFloat majorRadius NS_AVAILABLE_IOS(8_0);
@property(nonatomic,readonly) CGFloat majorRadiusTolerance NS_AVAILABLE_IOS(8_0);

@property(nonatomic,readonly,retain) UIWindow    *window; // 触摸产生时所处的窗口:
@property(nonatomic,readonly,retain) UIView      *view; // 触摸产生时所处的视图:
@property(nonatomic,readonly,copy)   NSArray     *gestureRecognizers NS_AVAILABLE_IOS(3_2);
说明:UITouchPhase枚举的定义:
typedef NS_ENUM(NSInteger, UITouchPhase) {
    UITouchPhaseBegan,             // whenever a finger touches the surface.
    UITouchPhaseMoved,             // whenever a finger moves on the surface.
    UITouchPhaseStationary,        // whenever a finger is touching the surface but hasn't moved since the previous event.
    UITouchPhaseEnded,             // whenever a finger leaves the surface.
    UITouchPhaseCancelled,         // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face)
};
2)方法
/* 返回值表示触摸在view上的位置,这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0)),调用时传入的view参数为nil的话,
 返回的是触摸点在UIWindow的位置。 */
- (CGPoint)locationInView:(UIView *)view;
/* 该方法记录了前一个触摸点的位置 */
- (CGPoint)previousLocationInView:(UIView *)view;

5. UIEvent类详解
事件就是UIEvent对象。它用来记录事件产生的时刻和类型。
1)属性
/* 事件的类型 */
@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;

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

touchesBegan…

touchesMoved…

touchedEnded…
注意:如果父控件不能接受触摸事件,那么子空间就不能接收到任何触摸事件。

7.响应者链的事件传递过程
(1)如果view的控制器存在,就传递给控制器;如果控制器不存在,则将其传递给它的父视图。
(2)在视图层次结构的最顶级视图,如果也不能处理收到的事件或消息,则其将事件或消息传递给window对象进行处理。
(3)如果window对象也不处理,则其将事件或消息传递给UIApplication对象。
(4)如果UIApplication也不能处理该事件或消息,则将其丢弃。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值