iOS事件传递与响应

触摸事件发生时,会递归调用hitTest:withEvent获得响应事件的试图,然后将触摸事件包装成UITouch,传递给[UIWindow hitTest:withEvent]调用返回的试图处理
• hitTest:withEvent:方法大致处理流程是这样的:
首先调用当前视图的pointInside:withEvent:方法判断触摸点是否在当前视图内:
▶ 若pointInside:withEvent:方法返回NO,说明触摸点不在当前视图内,则当前视图的hitTest:withEvent:返回nil
▶ 若pointInside:withEvent:方法返回YES,说明触摸点在当前视图内,则遍历当前视图的所有子视图(subviews),调用子视图的hitTest:withEvent:方法重复前面的步骤,子视图的遍历顺序是从top到bottom,即从subviews数组的末尾向前遍历,直到有子视图的hitTest:withEvent:方法返回非空对象或者全部子视图遍历完毕:
▷ 若第一次有子视图的hitTest:withEvent:方法返回非空对象,则当前视图的hitTest:withEvent:方法就返回此对象,处理结束
▷ 若所有子视图的hitTest:withEvent:方法都返回nil,则当前视图的hitTest:withEvent:方法返回当前视图自身(self)
• 最终,这个触摸事件交给主窗口的hitTest:withEvent:方法返回的视图对象去处理
我大致画了个iOS触摸事件分发的原理图:


iOS事件传递与响应_第1张图片

• hitTest:withEvent:方法会忽略以下视图:
1> 隐藏(hidden=YES)的视图

2> 禁止用户操作(userInteractionEnabled=NO)的视图

3> alpha<0.01的视图

4> 如果一个子视图的区域超过父视图的区域(如果父视图的clipsToBounds属性为NO,超过父视图区域的子视图内容也会显示),那么正常情况下在父 视图区域外的触摸操作不会被识别,因为父视图的pointInside:withEvent:方法会返回NO,这样就不会继续向下遍历子视图了。当然,也 可以重写pointInside:withEvent:方法来处理这种

综上所述可得:如果父视图的userInteractionEnabled=NO,触摸事件不会继续往下传递给子视图,所以子视图永远无法处理触摸事件。而UIImageView在默认情况下的userInteractionEnabled就是NO。

事件的反向传递:

重写视图的touchesBegan方法,打印nextResponder,查看输出

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSMutableString *str = [[NSMutableString alloc] init];
    printf("%s%s\n",str.UTF8String,NSStringFromClass([self class]).UTF8String);
    UIResponder *nextResponder = self.nextResponder;
    while (nextResponder) {
        printf("%s%s\n",str.UTF8String,NSStringFromClass([nextResponder class]).UTF8String);
        nextResponder = nextResponder.nextResponder;
        [str appendString:@"--"];
    }
}
iOS事件传递与响应_第2张图片
6EC0EA82-7ADA-4797-BD87-DF2C35CD3339.png

EventView的控制器是EnventChuandiViewController,父视图是NextResponderView,NextResponderView的控制器是MethodSwizzlingViewController

hintTest是UIView的方法,所以产生event后寻找的入口是UIWindow。
但是当事件反向传递的时候,会传到UIApplication和AppDelegte.

那么问题来啦
1,responder是什么时候被赋值的?
2,hitTest的实现是什么?
3,UIScoroll如何响应事件?

responder是什么时候被赋值的?

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    EnventChuandiViewController *eventViewVC = [[EnventChuandiViewController alloc] init];
//(lldb) po eventViewVC.view.nextResponder
//
//(lldb) po eventViewVC.nextResponder
// nil
    [self.view addSubview:eventViewVC.view];
//(lldb) po eventViewVC.nextResponder
//>
    [self addChildViewController:eventViewVC];
    [eventViewVC didMoveToParentViewController:self];
    eventViewVC.view.frame = CGRectMake(100, 100, 100, 100);
    UIImageView *view = [[UIImageView alloc] init];
(lldb) po view.nextResponder
// nil
    [self.view addSubview:view];
(lldb) po view.nextResponder
//>
}

可以看到在创建视图和addSubView的时候fuzhi

hintTest实现如下:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (!self.userInteractionEnabled || self.alpha < 0.01 || ![self pointInside:point withEvent:event] || self.isHiden) {
        return nil;
    }
    for (UIView *view in [self.subviews reverseObjectEnumerator]) {
       UIView *viewTmp = [view hitTest:[view convertPoint:point fromView:self] withEvent:event];
        if (viewTmp) {
            return viewTmp;
        }
    }
    return self;
}

为证明,给UIView写一个分类替换掉系统实现:

#import 

@interface UIView (hint)

@end
#import "UIView+hint.h"
#import "objc/runtime.h"
@implementation UIView (hint)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL originalSelector = @selector(hitTest:withEvent:);
        SEL swizzledSelector = @selector(myhitTest:withEvent:);
        Class class = [self class];
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }

    });
}
- (UIView *)myhitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (!self.userInteractionEnabled || self.alpha < 0.01 || ![self pointInside:point withEvent:event] || self.isHidden) {
        return nil;
    }
 
    for (UIView *view in [self.subviews reverseObjectEnumerator]) {
        UIView *viewTmp = [view hitTest:[view convertPoint:point fromView:self] withEvent:event];
        if (viewTmp) {
            return viewTmp;
        }
    }
    return self;
}

@end

系统运行正常
3,UIScoroll如何响应事件?
视图上默认添加的手势识别器会拦截触摸事件
(1),触摸时间小于阈值
如果滑动了手指,UIScrollView响应滑动事件
如果没滑动过,不作处理
(2),触摸时间大于阈值
如果没有滑动过手指,那么会把触摸事件交给子视图处理(如果之后手指滑动,会取消发送给子视图的事件,UIScrollView响应滑动事件)
如果滑动过手指,UIScrollView继续响应滑动事件,事件不会传递给子视图

你可能感兴趣的:(iOS事件传递与响应)