iOS-触摸事件

来源:互联网 发布:胜利足彩欧赔数据库 编辑:程序博客网 时间:2024/06/11 22:47
  1. ios中的事件概述
    在ios中会有各种各样的事件,它们可以分为3类:触摸事件、加速计事件、远程控制事件。

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

  3. 对UIResponder的介绍
    1)4个触摸事件

    • (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的个数可以判断出是单点触摸还是多点触摸。
      2)3个加速计事件
    • (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);
      3)1个远程控制事件
    • (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;

  1. 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…
注意:如果父控件不能接受触摸事件,那么子空间就不能接收到任何触摸事件。

0 0
原创粉丝点击