尋找被觸摸的View.jpg
在iOS系統(tǒng)中,當用戶觸摸了一個view后,一個完整的事件響應是分為兩個過程的:
- 尋找被觸摸的view;
- 處理觸摸事件;
在本篇文章中我要介紹的是第一個過程。
每個UIView都有一個subViews數組(UIWindow也是UIView),最先添加的subView成為其第0個元素,后來添加的今次成為第1,2,...個元素。
每個UIView都有方法一:-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event ;
和方法二:-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
下面我們將以這個例子來闡述:
Paste_Image.png
view0為ViewController的view,view1和view2被添加在view0上(先添加view1再添加view2)。
如果用戶點擊了view1。
- 系統(tǒng)會檢測到手指觸摸事件并將其放入當前活動Application的事件隊列中,UIApplication會從事件隊列中取出觸摸事件并傳遞給key window ;
- 然后key window會執(zhí)行
方法一
,該方法會先調用方法二
,此時由于觸摸點是在key window的范圍內,方法二
會返回YES;然后key window會給view0發(fā)hitTest:消息(即讓view0執(zhí)行方法一
),其方法一
又調用其方法二
,方法二還是會返回YES; - 然后view0會給view2發(fā)hitTest:消息(view2執(zhí)行其
方法一
),調用其方法二
,此時由于點擊的是view1,觸摸點不在view2的范圍內,方法二
會返回NO,view2的方法一
返回nil; - 然后view0會再給view1發(fā)hitTest:消息(view1執(zhí)行其
方法一
),調用其方法二
,觸摸點在view1范圍內,方法二
返回YES,由于view1的subViews中沒有元素了,其方法一
將view1自己返回。此時,view1就作為被觸摸的view被找到了。
如果用戶點擊了view0(觸摸點不在view1或view2上)。
- view1的view2的
方法二
都返回NO,它們的方法一
都返回nil,此時,view0的方法一
將view0返回,view0作為觸摸的view被找到了。
view找到了,我們來作個總結:
- 首先調用當前視圖的pointInside:withEvent:方法判斷觸摸點是否在當前視圖內;
- 若返回NO,則hitTest:withEvent:返回nil;
- 若返回YES,則向當前視圖的所有子視圖(subviews)發(fā)送hitTest:withEvent:消息,所有子視圖的遍歷順序是從top到bottom,即從subviews數組的末尾向前遍歷,直到有子視圖返回非空對象或者全部子視圖遍歷完畢;
- 若有子視圖返回非空對象,則hitTest:withEvent:方法返回此對象,處理結束(不再對其它子視圖發(fā)送hitTest:消息);
- 如所有子視圖都返回非,則hitTest:withEvent:方法返回自身(self)。
* 來自——hitTest:withEvent:方法流程 *
最后,我們再來為其寫一個應用實例,
Paste_Image.png
如圖:topview作為ViewController的view,view1,view2都是topView的子視圖,但view1覆蓋在view2。
如果不作處理,我們是沒法點擊到view2的,因為被view1遮住了,這里我們設置view1的alpha為0.8,方便我們觀察。那么我們就來處理這個問題吧!
ViewController.m
@interface ViewController ()
@property(strong, nonatomic)UIView *mView0;
@property(strong, nonatomic)UIView *mView1;
@property(strong, nonatomic)TestView0 *mTopView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_mTopView = [[TestView0 alloc]initWithFrame:self.view.bounds];
self.view = _mTopView;
}
@end
TestView0.m
#import "TestView0.h"
@interface TestView0()
@property(strong, nonatomic)UIButton *mView1;
@property(strong, nonatomic)UIButton *mView2;
@end
@implementation TestView0
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
_mView1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];
_mView2 = [[UIButton alloc]initWithFrame:CGRectMake(20, 100, 200, 200)];
[_mView1 addTarget:self action:@selector(view1Action) forControlEvents:UIControlEventTouchUpInside];
[_mView2 addTarget:self action:@selector(view2Action) forControlEvents:UIControlEventTouchUpInside];
_mView1.backgroundColor = [UIColor orangeColor];
_mView2.backgroundColor = [UIColor greenColor];
_mView1.alpha = 0.8;
[self addSubview:_mView2];
[self addSubview:_mView1];
}
return self;
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *result = [super hitTest:point withEvent:event];
CGPoint clickPoint = [_mView2 convertPoint:point fromView:self];
if ([_mView2 pointInside:clickPoint withEvent:event]) {
return _mView2;
}
return result;
}
-(void)view1Action{
self.backgroundColor = [UIColor purpleColor];
}
-(void)view2Action{
self.backgroundColor = [UIColor redColor];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
self.backgroundColor = [UIColor whiteColor];
}
@end ```
結果:



參考:[hitTest:withEvent:方法流程](http://blog.csdn.net/jiajiayouba/article/details/23447145)