iOS响应者链之事件传递与事件响应

2018-04-22  本文已影响32人  conowen

概述

iOS中不是任何对象都能处理事件, 只有继承了UIResponder的对象才能接收并处理事件,我们称为响应者对象。UIApplication,UIViewController,UIView都继承自UIResponder,因此他们都是响应者对象, 都能够接收并处理事件。而这些事件可以分为以下三种,本文主要简单聊聊屏幕触摸事件。包括事件传递事件响应

//触摸事件
- (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;

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

//远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

事件传递

一般事件的传递是从父控件传递到子控件的,这个过程可以简单成为hit test过程,字面上意思大概是『点击测试』。
过程如下
UIApplication->Window->父view->子View->下一个View

注意:如果父控件接受不到触摸事件,那么子控件就不可能接收到触摸事件

UIView不能接收触摸事件的三种情况:
userInteractionEnabled = NO;
hidden = YES;
alpha <= 0.01

这个过程主要是UIView的两个函数来实现寻找合适view。

- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;   
// 如果坐标点在view的bounds内,就递归调用 -pointInside:withEvent:方法,返回最合适view

- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;  
 // 如果坐标点在view的bounds内,就返回YES

hitTest函数大概原理如下

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//如果隐藏、透明度<=0.01或者不接受用户交互都返回nil,表示这个view不是合适的view
    if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01) {
        return nil;
    }
//如果坐标点在view 的bounds内,就遍历所有子view
    if ([self pointInside:point withEvent:event]) {
        //倒序处理子view
        for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
            //坐标转换,把当前控件上的坐标系转换成子控件上的坐标系
            CGPoint convertedPoint = [subview convertPoint:point fromView:self];
            //递归判断子view的hit test
            UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
            if (hitTestView) {
                return hitTestView;
            }
        }
//如果都不符合,返回自己
        return self;
    }
    return nil;
}

这里要注意一点,之所以是倒序处理子view,先处理后添加的子view。这样做是为了提高递归效率,因为一般后添加的子view在父view的层级关系里面的最上面,更容易找到更合适的view。

示例

如下图所示,如果点击view E,寻找最合适的view过程如下


image.png

Touch坐标点在ViewA的bounds中,递归检查ViewB和ViewC;
Touch坐标点不在ViewB的bounds中,检查ViewC;
Touch坐标点在ViewC的bounds中,检查ViewD和ViewE;
Touch坐标点不在ViewD中,检查ViewE;
Touch坐标点在ViewE中,所以ViewE为hit test的结果;
执行ViewE的touches方法,进行响应事件。

事件响应

上述说到的事件传递,大概是这样的过程UIApplication->Window->父view->子View->下一个View,经过hit test过程找到最合适的view之后。就开始事件响应,主要做的就是判断这个最合适的view有没有响应touch的相关方法。如果没有实现,就找上一个合适的view,一般是这个view的父类,看这个view有没有实现,如果一直没有找到的话,这个touch事件就作废抛弃了。

所以简单来说,事件传递是从父view到子view传递,而事件响应是从子view到父view响应的。

应用场景

所以,从你点击屏幕一个View开始,系统就会将UITouch和UIEvent对象打包, 放到当前活动的Application的事件队列中,单例的UIApplication会从事件队列中取出触摸事件并传递给单例UIWindow,UIWindow使用hitTest:withEvent:方法查找touch操作的所在的视图view。

以下应用场景主要就是修改这两个回调函数实现的

- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;   
// 如果坐标点在view的bounds内,就递归调用 -pointInside:withEvent:方法,返回最合适view

- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;  
 // 如果坐标点在view的bounds内,就返回YES

在最终响应的view中的touch事件处理方法直接调用上一级处理方法便可。

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  NSLog(@"do something");
  [super touchesBegan:touches withEvent:event];
}

参考

http://s1.downloadmienphi.net/file/downloadfile4/147/1389777.pdf

上一篇 下一篇

猜你喜欢

热点阅读