iOS中的事件,手势识别

来源:互联网 发布:网络攻防知识 编辑:程序博客网 时间:2024/06/11 08:06

iOS中的事件可以分为三大类型:

1.触摸事件

触摸事件的传递是从父控件到子控件,如果父控件不能接收触摸事件,那么子控件就不可能接收到触摸事件

不能接收触摸事件的几种情况:1.userInteractionEnabled = NO; 2.hidden = yes; 3.alpha = 0.0--0.01

//touches中存放的是UITouch对象

//触摸事件- (void)touchBegin:(NSSet *)touches withEvent:(UIEvent *)event- (void)touchMoved:(NSSet *)touches withEvent:(UIEvent *)event- (void)touchEnded:(NSSet *)touches withEvent:(UIEvent *)event
//比如有电话呼入,会导致触摸事件停止,这时候会调用该方法- (void)touchCancelled:(NSSet *)touched withEvent:(UIEvent *)event
<pre class="objc" name="code">//一次完整的触摸过程,会经历3个状态://触摸开始:- (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//4个触摸事件处理方法中,都有NSSet *touches和UIEvent *event两个参数//一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数//如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象//如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象//根据touches中UITouch的个数可以判断出是单点触摸还是多点触摸


2.加速计事件

//加速剂事件- (void)motionBegin:(UIEventSubtype)motion withEvent:(UIEvent *)event- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

3.远程控制事件

- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

只有继承了UIResponder的对象才可以接受并处理事件,又叫“响应者对象”


UITouch 的一些属性


widow 触摸产生所在的窗口

view  触摸产生所在的视图

tapCount 短时间内点按屏幕的次数

timestamp 记录了触摸产生或者变化的时间

phase 触摸事件所处的状态
UITouchPhase是一个枚举类型,包含:UITouchPhaseBegan(触摸开始)UITouchPhaseMoved(接触点移动)UITouchPhaseStationary(接触点无移动)UITouchPhaseEnded(触摸结束)UITouchPhaseCancelled(触摸取消)
- (CGPoint)locationInView:(UIView *)view;//返回值表示触摸在view上的位置//这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))//调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置- (CGPoint)previousLocationInView:(UIView *)view;//该方法记录了前一个触摸点的位置
UIEvent 
没产生一个事件,就是一个UIEvent对象,记录了事件产生的时间和类型
<pre class="objc" name="code">//常见属性//事件类型@property(nonatomic,readonly) UIEventType     type;@property(nonatomic,readonly) UIEventSubtype  subtype;/*
typedef NS_ENUM(NSInteger, UIEventType) {    UIEventTypeTouches,//触摸事件    UIEventTypeMotion,//加速计事件    UIEventTypeRemoteControl,//远程事件};typedef NS_ENUM(NSInteger, UIEventSubtype) {    // available in iPhone OS 3.0    UIEventSubtypeNone                              = 0,        // for UIEventTypeMotion, available in iPhone OS 3.0    UIEventSubtypeMotionShake                       = 1,        // for UIEventTypeRemoteControl, available in iOS 4.0    UIEventSubtypeRemoteControlPlay                 = 100,    UIEventSubtypeRemoteControlPause                = 101,    UIEventSubtypeRemoteControlStop                 = 102,    UIEventSubtypeRemoteControlTogglePlayPause      = 103,    UIEventSubtypeRemoteControlNextTrack            = 104,    UIEventSubtypeRemoteControlPreviousTrack        = 105,    UIEventSubtypeRemoteControlBeginSeekingBackward = 106,    UIEventSubtypeRemoteControlEndSeekingBackward   = 107,    UIEventSubtypeRemoteControlBeginSeekingForward  = 108,    UIEventSubtypeRemoteControlEndSeekingForward    = 109,};*///事件产生的时间@property(nonatomic,readonly) NSTimeInterval  timestamp;
//手势识别
<pre class="objc" name="code">利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势UITapGestureRecognizer(敲击)UIPinchGestureRecognizer(捏合,用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)

使用:
//创建手势识别器对象UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];//设置手势识别器对象的具体属性// 连续敲击2次tap.numberOfTapsRequired = 2;// 需要2根手指一起敲击tap.numberOfTouchesRequired = 2;//添加手势识别器到对应的view上[self.iconView addGestureRecognizer:tap];//监听手势的触发[tap addTarget:self action:@selector(tapIconView:)];

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {    // 没有触摸事件发生,所有手势识别的默认状态    UIGestureRecognizerStatePossible,    // 一个手势已经开始但尚未改变或者完成时    UIGestureRecognizerStateBegan,    // 手势状态改变    UIGestureRecognizerStateChanged,    // 手势完成    UIGestureRecognizerStateEnded,    // 手势取消,恢复至Possible状态    UIGestureRecognizerStateCancelled,     // 手势失败,恢复至Possible状态    UIGestureRecognizerStateFailed,    // 识别到手势识别    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded};



0 0