iOS 响应者链

2017-02-20  本文已影响35人  盘石垂钓

摘要

触摸事件的传递,事件的响应

事件的产生

Paste_Image.png

怎么在视图层级中找到合适的视图

view有三种情况不响应触摸事件。

找到合适视图的具体过程

真个过程就是从最顶层的视图开始通过递归找到最终的的合适的视图。

hitTest

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{

view使用hitTest检测自己是不是应该响应改触摸或者改event传递给子视图去检测。
里边使用了递归。

下边我们自己实现的一个重写的hitTest方法。

#import "WYPResponderView.h"

@implementation WYPResponderView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    //输出tag
    NSLog(@"******%ld****",self.tag);
    if (self.userInteractionEnabled == NO || self.hidden == YES ||  self.alpha <= 0.01) {
        return nil;
    };
    if ([self pointInside:point withEvent:event] == NO) return nil;
    NSInteger count = self.subviews.count;
   //从后往前遍历子视图
    for (NSInteger i = count - 1; i >= 0; i--)     {
        UIView *childView = self.subviews[i];
        CGPoint childP = [self convertPoint:point toView:childView];
        UIView *foundView = [childView hitTest:childP withEvent:event];
        if (foundView) {
            return foundView;
        }
    }
    return self;
}

@end

我们可以检测下上述代码的正确性并且跟不重写hitTest方法的结果比较下。

创建几个视图,并且给每个视图设置不同的tag。

- (UIView *)generateView
{
    WYPResponderView * mainView = [[WYPResponderView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    mainView.backgroundColor = [UIColor redColor];
    mainView.tag = 0;
    [self.view addSubview:mainView];
    
    WYPResponderView *view1 = [[WYPResponderView alloc] initWithFrame:CGRectMake(20, 30, SCREEN_WIDTH - 80, 250)];
    view1.backgroundColor = [UIColor greenColor];
    view1.tag = 1;
    [mainView addSubview:view1];
    
    WYPResponderView *view2 = [[WYPResponderView alloc] initWithFrame:CGRectMake(20, 300, SCREEN_WIDTH - 80, 250)];
    view2.backgroundColor = [UIColor blueColor];
    [mainView addSubview:view2];
    view2.tag = 2;
    
    
    WYPResponderView *view1_1 = [[WYPResponderView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 80 - 20, 100)];
    view1_1.backgroundColor = [UIColor grayColor];
    view1_1.tag = 3;
    [view1 addSubview:view1_1];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
    button.userInteractionEnabled = NO;
    [button addTarget:self action:@selector(XX) forControlEvents:UIControlEventTouchDown];
    button.backgroundColor = [UIColor purpleColor];
    
    [view1_1 addSubview:button];
    
    WYPResponderView *view1_2 = [[WYPResponderView alloc] initWithFrame:CGRectMake(10, 130, SCREEN_WIDTH - 80 - 20, 100)];
    view1_2.backgroundColor = [UIColor darkGrayColor];
    view1_2.tag = 4;
    [view1 addSubview:view1_2];
    
    
    WYPResponderView *view2_1 = [[WYPResponderView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 80 - 20, 50)];
    view2_1.backgroundColor  = [UIColor grayColor];
    view2_1.tag = 5;
    [view2 addSubview:view2_1];
    
    UIView *view2_2 = [[WYPResponderView alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH - 80 - 20, 170)];
    view2_2.backgroundColor = [UIColor yellowColor];
    view2_2.tag = 6;
    [view2 addSubview:view2_2];
    
    UIView *view2_2_1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH - 150, 100)];
    view2_2_1.backgroundColor = [UIColor purpleColor];
    view2_2_1.tag = 7;
    [view2_2 addSubview:view2_2_1];
    
    return mainView;
}

界面的最终呈现。

Paste_Image.png

点击view2_2_1时的hitTest过程

Paste_Image.png

如果我们修改hitTest方法如下,在默认的hitTest方法上输出一段内容。

Paste_Image.png

再次点击下view2_2_1
可以看到输出跟我们重写hitTest检测过程是一样的。

事件的响应

可以用一张苹果的官方图说明。

Paste_Image.png

如果我们找到合适的view之后,则根据view的touchesBegan方法判断view是否要处理该事件,如果不处理则将事件抛给下一个响应者去处理。touchesBegan的默认方法就是传递给下一个响应者。

如何找到下一个响应者

上一篇 下一篇

猜你喜欢

热点阅读