UIEvent - 3

对上一节的例子做一些修改:

LBEventTestView.h

#import <UIKit/UIKit.h>

@interface LBEventTestView : UIView

@property (nonatomic, strong) NSString* colorString;//增加辨识变量colorString

@end

LBEventTestView.m

#import "LBEventTestView.h"

@implementation LBEventTestView

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    
    NSLog(@"ColorString hitTest : %@,  %@", self.colorString, [super hitTest:point withEvent:event]);
    
    return [super hitTest:point withEvent:event];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    NSLog(@"ColorString pointInside : %@,  %@", self.colorString, [super pointInside:point withEvent:event]? @"YES" : @"NO");
    
    return [super pointInside:point withEvent:event];
}

@end


调用的视图控制器:

#import "Masonry.h"
#import "LBEventTestView.h"
#import "UIEventTestController.h"

@implementation UIEventTestController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //RED
    LBEventTestView* redView = [[LBEventTestView alloc] init];
    redView.colorString = @"RED";
    [redView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:redView];
    
    [redView mas_makeConstraints:^(MASConstraintMaker* make){
    
        make.center.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(250, 350));
    }];
    
    
    //YELLOW
    LBEventTestView* yellow = [LBEventTestView new];
    yellow.colorString = @"YELLOW";
    [yellow setBackgroundColor:[UIColor yellowColor]];
    [redView addSubview:yellow];
    
    [yellow mas_makeConstraints:^(MASConstraintMaker* make){
        
        make.centerX.equalTo(redView);
        make.size.mas_equalTo(CGSizeMake(100, 80));
        make.top.mas_equalTo(redView.mas_top).with.offset(30);
        
    }];
    
    //GREEN
    LBEventTestView* green = [LBEventTestView new];
    green.colorString = @"GREEN";
    [green setBackgroundColor:[UIColor greenColor]];
    [redView addSubview:green];
    
    [green mas_makeConstraints:^(MASConstraintMaker* make){
    
        make.centerX.equalTo(redView);
        make.size.mas_equalTo(yellow);
        make.bottom.mas_equalTo(redView.mas_bottom).with.offset(-30);
    
    }];
}

@end

如图:


分别点击这4个区域然后参考上文的文章理解。


挑选一个点击区域1和区域4来讲解:

区域1:


解析:点击白色区域1,开始遍历子视图的时候发现点击点不在RED红色区域(因为return的都是NO),就不继续调用别的在RED相关的子视图了。那一定会问为什么会调用了两次? 如图上所示是两次,看看文档的方法描述:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;   // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system

就是这个方法搞的鬼,它是用递归式的调用。所以调用的是多次,具体是多少次,我还真不敢说。(因为不懂哈哈)


区域4:


可以清楚看到,yellow视图同为RED的子视图,是完全不会去调用的。连Return NO的机会都没有。慢慢的似乎有一点点的规律,上一节的两层视图的结构就会调用2次,这一节的3层视图的结构就会调用6次,我严重怀疑recursively是不是 视图继承层数 H * 2 呢。以后懂了再在这里阐述吧!





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值