美文网首页
iOS中的事件

iOS中的事件

作者: 简直帅到飞 | 来源:发表于2017-02-13 11:48 被阅读0次

    在iOS中不是所有对象都能处理事件,只有继承了UIResponder的对象才能处理事件,我们称之为"响应者对象"

    事件分类

    • 触摸事件
    • 加速计事件
    • 远程控制事件

    触摸事件

    // 系统自动调用
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
    // 该方法会持续调用
    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
    // 手指离开屏幕时调用
    - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
    // 事件中断时时调用
    - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
    - (void)touchesEstimatedPropertiesUpdated:(NSSet<UITouch *> *)touches NS_AVAILABLE_IOS(9_1);
    

    加速计事件

    - (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
    

    远程控制事件

    - (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);
    

    UITouch

    • 一个手指对应一个UITouch对象
    • UITouch的作用
    1. 保存跟手指相关的信心,比如触摸的位置,事件,阶段
    2. 当手指移动时,系统会一直更新该UITouch对象,使之能够一直保存该手指的触摸位置
    3. 当手指离开屏幕时,系统会自动销毁该UITouch对象
    • 获取手指触摸位置
    UITouch *touch = [touches anyObject];
    // 当前触摸的点
    CGPoint currentP = [touch locationInView:self];
    // 上一个触摸的点
    CGPoint previousP = [touch previousLocationInView:self];
    

    事件的产生和传递

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

    事件传递的完整过程

    • 先将事件对象由上往下传递,找到最合适的控件来处理这个事件
    • 调用最合适控件的touches方法
    • 如果调用了[super touche..]方法,就会将事件顺着响应者链条向上传递,传递给上一个响应者
    1. 先去判断当前的view是不是控制器的view,如果是,那么它的上一个响应者就是它的控制器
    2. 如果当前的view不是控制器的view,那么它的上一个响应者就是它的父控件
    • 接着会调用上一个响应者的touches方法

    控件不能处理事件的原因

    • 如果父控件不能处理事件,那么它的子控件也不能接收处理事件
    • userInteractionEnabled = NO // 不接收用户交互
    • hidden = YES (当一个控件隐藏的时候,它的子控件也会跟随父控件一起隐藏)
    • alpha = 0.0~0.01 (当一个控件透明的时候,它的子控件也会跟随父控件一起透明)

    如何找到最适合的控件来处理事件

    • 自己是否能接收触摸事件
    • 触摸点是否在自己身上
    • 从后往前遍历子控件,重复以上两个步骤
    • 如果没有符合条件的子控件,那么自己是最合适的
    
    // 作用:去寻找最合适的View
    // 什么时候调用:当一个事件传递给当前view就会调用该方法
    // 返回的是谁,谁就是最适合的view
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        return nil;
    }
    

    hitTest方法的底层实现

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        // 判断自己能否处理触摸事件
        if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
            return nil;
        }
        
        // 判断点是否在自己身上
        if (![self pointInside:point withEvent:event]) {
            return nil;
        }
        
        // 从后往前遍历子控件,重复前两步操作
        NSInteger count = self.subviews.count;
        for (NSInteger i = count -1; i >= 0; i--) {
            // 取出每一个view
            UIView *childView = self.subviews[i];
            // 转换坐标系
            CGPoint childP = [self convertPoint:point toView:childView];
            UIView *fitView = [childView hitTest:childP withEvent:event];
            
            // 判断是否找到合适的view
            if (fitView) {
                return fitView;
            }
        }
        
        // 否则自己就是最合适的view
        return self;
    }
    
    // 判断当前的点在不在调用它的view上
    // 调用时间: 在hitTest方法中调用
    // point点必须要跟它调用者在同一个坐标系里面
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        return YES;
    }
    

    UIGestureRecognizer手势识别

    • UIGestureRecognizer它是一个抽象类
    • UITapGestureRecognizer(敲击)
    • UIPinchGestureRecognizer(捏合)
    // 获取缩放比例
    CGFloat scale = tap.scale;
    // 复位操作
    [pinch setScale:1];
    
    • UISwipeGestureRecognizer(轻扫)
    • 分方向direction
    UISwipeGestureRecognizerDirectionRight
    UISwipeGestureRecognizerDirectionLeft
    UISwipeGestureRecognizerDirectionUp  
    UISwipeGestureRecognizerDirectionDown
    
    • UIPanGestureRecognizer(拖拽)
    // 相对于最原始的偏移量
    CGPoint point = [pan translationInView:view];
    // 复位操作
    [pan setTranslation:CGPointZero inView:view];
    
    • UILongPressGestureRecognizer(长按)
    • 长按移动时,会持续调用目标方法
    • 长按手势分状态:state
     UIGestureRecognizerStatePossible
     UIGestureRecognizerStateBegan
     UIGestureRecognizerStateChanged
     UIGestureRecognizerStateEnded
     UIGestureRecognizerStateCancelled
     UIGestureRecognizerStateFailed
     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
    
    • UIRotationGestureRecognizer(旋转)
    // 获取缩放角度
    CGFloat rotation = tap.rotation;
    // 复位操作
    [tap setRotation:0];
    
    • 多个手势操作
    // 该代理方法用来判断是否同时支持多个手势操作
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
    

    添加手势方法

    UIView *view = [[UIView alloc] init];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    // 设置代理
    tap.delegate = self;
    [view addGestureRecognizer:tap];
    
    // 该代理方法可以用来设置可以点击的区域
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        
        return YES;
    }
    

    相关文章

      网友评论

          本文标题:iOS中的事件

          本文链接:https://www.haomeiwen.com/subject/wdmaittx.html