IOS中的触摸事件
2015-12-10 17:31
381 查看
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; 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也不能处理该事件或消息,则将其丢弃。
相关文章推荐
- iOS 十进制 二进制 转换
- 需求 - 12 - 图片缓存
- ios 学习笔记之控件属性
- 模型的应用 && Xib &&代理模式的应用【应用管理】
- ios 实现图片上传的2种方式
- 【iOS 开发】如何去除tableView多余的空Cell
- Teemo__Tip1__iOS界面跳转及刷新
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- iOS多线程编程之NSThread的使用
- iOS 学习日志 :利用AFNetWorking 做文件上传
- mask遮障效果实现注水动画
- iOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法:接口转换法,代码转换法
- ios 学习总结之动画(转)
- iOS 学习日志:调用相机拍照获得图片旋转问题
- 处理iOS报错: Warning “attempt to present ViewController whose view is not in the window hierarchy”
- ios 中的autoresizingMask(转载)
- 使用IOS企业版证书发布应用
- iOS开发融云即时通讯集成详细步骤
- iOS开发之CGRect常用指南
- iOS中assign,copy,retain之间的区别以及weak和strong的区别