关于OC中的事件传递与响应链已有很多文章讲述,例如:
深入浅出iOS事件机制
关于响应者链,也就如此。
看完这些文章对于其过程也有了大致的理解,那么接下来让我们进行一些测试,加深印象:
TEST_1
让我们在新建的ViewController中,添加一些label:
// ...
/// 响应链测试View
@property ( nonatomic, strong ) UILabel *AA;
@property ( nonatomic, strong ) UILabel *AAA;
@property ( nonatomic, strong ) UILabel *BB;
@property ( nonatomic, strong ) UILabel *BBB;
@property ( nonatomic, strong ) UILabel *BBBB;
@property ( nonatomic, strong ) UILabel *BBBBB;
// ...
- (void)viewDidLoad {
[super viewDidLoad];
self.AA = [self labelWithString:@"AA"];
self.AA.backgroundColor = [UIColor lightGrayColor];
self.AA.frame = CGRectMake(10, 20, 300, 200);
self.AAA = [self labelWithString:@"AAA"];
self.AAA.backgroundColor = [UIColor grayColor];
self.AAA.frame = CGRectMake(50, 50, 100, 100);
self.BB = [self labelWithString:@"BB"];
self.BB.backgroundColor = [UIColor lightGrayColor];
self.BB.frame = CGRectMake(10, 250, 300, 300);
self.BBB = [self labelWithString:@"BBB"];
self.BBB.backgroundColor = [UIColor grayColor];
self.BBB.frame = CGRectMake(50, 0, 200, 200);
self.BBBB = [self labelWithString:@"BBBB"];
self.BBBB.backgroundColor = [UIColor darkGrayColor];
self.BBBB.frame = CGRectMake(50, 0, 180, 180);
self.BBBBB = [self labelWithString:@"BBBBB"];
self.BBBBB.backgroundColor = [UIColor greenColor];
self.BBBBB.frame = CGRectMake(10, 0, 100, 50);
[self.view addSubview:self.BB];
[self.BB addSubview:self.BBB];
[self.BBB addSubview:self.BBBB];
[self.BBBB addSubview:self.BBBBB];
[self.view addSubview:self.AA];
[self.AA addSubview:self.AAA];
}
- (UILabel *)labelWithString:(NSString *)string{
UILabel *label = [[UILabel alloc]init];
label.text = string;
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTarget:)];
[label addGestureRecognizer:tap];
return label;
}
- (void)viewTarget:(UITapGestureRecognizer *)tap{
UILabel *label = (UILabel *)tap.view;
NSLog(@"%@",label.text);
}
看看运行之后的样子:
我们要对上面的几个地方进行点击测试,查看输出结果。
让我们先来推导一下输出的结果
拍脑袋的思考结果:
1 ---> AAA
2 ---> AA
3 ---> BBBBB
4 ---> BBBB
5 ---> BBBB
6 ---> BBB
7 ---> BB
好了 让我们走起来:
实际输出结果:
1 ---> AAA
2 ---> AA
3 ---> BBBBB
4 ---> BBBB
5 ---> BB
6 ---> BBB
7 ---> BB
啥情况?
5点虽然看上去在BBBB的范围内,却不在BBB的范围中,因此在BBB的
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
中已经返回了NO
,BB除了BBB外没有其他子View,事件传递到BB中止,开始响应,也就传递不到BBBB上了,因此输出的是BB
而不是BBBB
弄清楚了这一点以后,我们要怎么让BBBB响应在5中的点击事件呢?后面再说...
TEST_2
让我们继承UILabel,并在其中做一些操作:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
return self;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
return YES;
}
然后在ViewController的创建Label的方法中修改一下(其实也就改了个类名):
// ...
- (UILabel *)labelWithString:(NSString *)string{
TestLabel *label = [[TestLabel alloc]init];
label.text = string;
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTarget:)];
[label addGestureRecognizer:tap];
return label;
}
看看运行后(同TEST_1,点击几个点)的输出情况:
1 ---> AA
2 ---> AA
3 ---> AA
4 ---> AA
5 ---> AA
6 ---> AA
7 ---> AA
我们发现事件全被AA拦截了,原因是self.view在遍历subview时是从数组的最后往前遍历的(果然最小的孩子最受宠),因此最后加到self.view上的AA能够最先拦截点击事件,并响应。
通过修改添加AA和BB的顺序可以确认这一情况(这里就不水了)
TEST_3
在子类中大致模拟一下系统的调用过程:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if (self.userInteractionEnabled
&& !self.hidden
&& [self pointInside:point withEvent:event]) {
// 使用reverseObjectEnumerator进行倒序遍历
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
UIView *responseView = [subview hitTest:convertedPoint withEvent:event];
if (responseView) {
return responseView;
}
}
return self;
}
return nil;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
if (CGRectContainsPoint(self.bounds, point)) {
return YES;
}
return NO;
}
然后运行,可以看到输出结果与TEST_1中的是相同的。
然后我们改造一下,让BBBB可以响应在5上的点击事件:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
if (CGRectContainsPoint(self.bounds, point)) {
return YES;
}
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
BOOL inside = [subview pointInside:convertedPoint withEvent:event];
if (inside) {
return YES;
}
}
return NO;
}
运行一下,完全达到了想要的效果
TEST_4
接下来我们测试一下userInteractionEnabled
对事件传递的影响:
self.BBBB.userInteractionEnabled = NO;
这样BBBB和BBBBB就不会响应点击事件啦:
输出结果
1 ---> AAA
2 ---> AA
3 ---> BBB
4 ---> BBB
5 ---> BBB
6 ---> BBB
7 ---> BB
好像有点不对劲啊? 5上的点击事件怎么成了BBB响应呢?
emm...虽然貌似不小心搞出了不规则的点击区域,但还是要弄清楚。
原来是我们修改点击区域方法
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
时,只对范围进行判断,所以将subview的范围也返回YES了,即使它已经设置了userInteractionEnabled
再改改:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
if (self.userInteractionEnabled
&& !self.hidden
&& CGRectContainsPoint(self.bounds, point)) {
return YES;
}
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
BOOL inside = [subview pointInside:convertedPoint withEvent:event];
if (inside) {
return YES;
}
}
return NO;
}
大功告成,到此大概模拟了系统对点击事件的处理过程:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if (self.userInteractionEnabled
&& !self.hidden
&& [self pointInside:point withEvent:event]) {
// 使用reverseObjectEnumerator进行倒序遍历
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
UIView *responseView = [subview hitTest:convertedPoint withEvent:event];
if (responseView) {
return responseView;
}
}
return self;
}
return nil;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
if (self.userInteractionEnabled
&& !self.hidden
&& CGRectContainsPoint(self.bounds, point)) {
return YES;
}
for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
BOOL inside = [subview pointInside:convertedPoint withEvent:event];
if (inside) {
return YES;
}
}
return NO;
}
流程如下:
说了这么多,到底有什么用呢?且听下回分解。