UIEvent - 3
来源:互联网 发布:如何注册淘宝旺旺 编辑:程序博客网 时间:2024/05/17 09:18
对上一节的例子做一些修改:
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 呢。以后懂了再在这里阐述吧!
0 0
- UIEvent - 3
- UIEvent
- UIEvent
- 关注 UIEvent
- 9.UIEvent
- UIEvent简介
- UIevent事件
- UIEvent,UIResponder
- UIEvent - 1
- UIEvent - 2
- iOS-UIEvent
- UIEvent | UITouch | timestamp
- IOS UIEvent小结
- IOS开发之UIEvent
- UIEvent 基本学习
- UITouch And UIevent
- 关于 UITouch,UIEvent,UIResponder
- UITouch 和 UIEvent 分析
- android屏幕适配攻略
- sqlite基本操作命令
- lodash 中文学习拾零之 Chain篇
- java多线程之synchronized和锁对象
- map容器的简单概述及入门的定义和使用方法
- UIEvent - 3
- 华为笔试.利用递归进行穷举123456的所有组合情况
- 如何解决cocos2dx 在android系统的音效bug
- Windows下CMD中文乱码问题解决方法
- android拨打电话
- 缓存淘汰算法
- .net c#重装系统打开原工程或者打开低版本工程编译时报“加载属性“OutputPath”失败”
- hdu 5421 回文树
- android之UEventObserver分析