iOS事件处理

2015-07-18  本文已影响1128人  木_木27

iOS事件分类以及相关方法

在iOS中事件分为三类:

触摸事件:通过触摸、手势进行触发(例如手指点击、缩放、旋转)
运动事件(加速事件):通过加速器进行触发(例如手机晃动)
远程控制事件:通过其他远程设备触发(例如耳机控制按钮)

苹果官方的事件描述

在iOS中并不是所有的类都能处理接收并事件,只有继承自UIResponder类的对象(响应者对象)才能处理事件(如我们常用的UIView、UIViewController、UIApplication都继承自UIResponder,它们都能接收并处理事件)。
在iOS中并不是所有的类都能处理接收并事件,只有继承自UIResponder类的对象才能处理事件(如我们常用的UIView、UIViewController、UIApplication都继承自UIResponder,它们都能接收并处理事件)。

//一根或者多根手指开始触摸view,系统会自动调用view的下面方法
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
//一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event;
//一根或者多根手指离开view,系统会自动调用view的下面方法
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event;
//触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法
-(void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event;
//warning touches中存放的都是UITouch对象
//运动开始时执行
-(void)motionBegan:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
//运动结束后执行
-(void)motionEnded:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
//运动被意外取消时执行
-(void)motionCancelled:(UIEventSubtype)motionwithEvent:(UIEvent*)event;
//接收到远程控制消息时执行
-(void)remoteControlReceivedWithEvent:(UIEvent*)event;

触摸产生时所处的窗口
@property(nonatomic,readonly,retain)UIWindow *window;
触摸产生时所处的视图
@property(nonatomic,readonly,retain)UIView *view;
短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击
@property(nonatomic,readonly) NSUInteger tapCount;
记录了触摸事件产生或变化时的时间,单位是秒
@property(nonatomic,readonly)NSTimeInterval timestamp;
当前触摸事件所处的状态
@property(nonatomic,readonly)UITouchPhase phase;

-(CGPoint)locationInView:(UIView*)view;
//返回值表示触摸在view上的位置,这里返回的位置是针对view的坐标系的(以view的左上角为原点(0,0)),调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
- (CGPoint)previousLocationInView:(UIView*)view;
该方法记录了前一个触摸点的位置

每产生一个事件,就会产生一个UIEvent对象
UIEvent:称为事件对象,记录事件产生的时刻和类型
常见属性:
事件类型
@property(nonatomic,readonly)UIEventType type;
@property(nonatomic,readonly)UIEventSubtype subtype;
事件产生的时间
@property(nonatomic,readonly)NSTimeInterval timestamp;
UIEvent还提供了相应的方法可以获得在某个view上面的触摸对象(UITouch)

事件处理机制

在iOS中发生触摸后,事件会加入到UIApplication事件队列,UIApplication会从事件队列取出最前面的事件并分发处理,通常先分发给应用程序主窗口,主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件,这也是整个事件处理过程的第一步。

控件不能接受事件的三种情况:
1.不接收用户交互

   userInteractionEnabled= NO
   2.隐藏
   hidden = YES
   3.透明
   alpha = 0.0~0.01

提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    //将触摸点转到自己的身上的坐标
     CGPoint btnP = [self convertPoint:point toView:_btn];
    // 判断下当前点在不在按钮,如果在按钮上,返回按钮
    if ([_btn pointInside:btnP withEvent:event]) { // 点在按钮上
        return NO;
    }else{
        return [super pointInside:point withEvent:event];
    }
}
//系统会调用该方法来寻找最合适处理事件的控件
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
   CGPoint chatP = [self convertPoint:point toView:_chatView];
    // 判断下点在不在chatView
    if ([_chatView pointInside:chatP withEvent:event]) {
        return _chatView;
    }else{
        return [super hitTest:point withEvent:event];
    }
}

事件处理的完整过程

上一篇 下一篇

猜你喜欢

热点阅读