尋找被觸摸的View.jpg
在iOS系統(tǒng)中禾怠,當(dāng)用戶觸摸了一個view后返奉,一個完整的事件響應(yīng)是分為兩個過程的:
- 尋找被觸摸的view;
- 處理觸摸事件;
在本篇文章中我要介紹的是第一個過程吗氏。
每個UIView都有一個subViews數(shù)組(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)會檢測到手指觸摸事件并將其放入當(dāng)前活動Application的事件隊列中,UIApplication會從事件隊列中取出觸摸事件并傳遞給key window 捂齐;
- 然后key window會執(zhí)行
方法一
蛮放,該方法會先調(diào)用方法二
,此時由于觸摸點是在key window的范圍內(nèi)奠宜,方法二
會返回YES包颁;然后key window會給view0發(fā)hitTest:消息(即讓view0執(zhí)行方法一
)瞻想,其方法一
又調(diào)用其方法二
,方法二還是會返回YES娩嚼; - 然后view0會給view2發(fā)hitTest:消息(view2執(zhí)行其
方法一
)蘑险,調(diào)用其方法二
,此時由于點擊的是view1岳悟,觸摸點不在view2的范圍內(nèi)佃迄,方法二
會返回NO,view2的方法一
返回nil贵少; - 然后view0會再給view1發(fā)hitTest:消息(view1執(zhí)行其
方法一
)呵俏,調(diào)用其方法二
,觸摸點在view1范圍內(nèi)滔灶,方法二
返回YES普碎,由于view1的subViews中沒有元素了,其方法一
將view1自己返回录平。此時随常,view1就作為被觸摸的view被找到了。
如果用戶點擊了view0(觸摸點不在view1或view2上)萄涯。
- view1的view2的
方法二
都返回NO绪氛,它們的方法一
都返回nil,此時涝影,view0的方法一
將view0返回枣察,view0作為觸摸的view被找到了。
view找到了燃逻,我們來作個總結(jié):
- 首先調(diào)用當(dāng)前視圖的pointInside:withEvent:方法判斷觸摸點是否在當(dāng)前視圖內(nèi)序目;
- 若返回NO,則hitTest:withEvent:返回nil;
- 若返回YES,則向當(dāng)前視圖的所有子視圖(subviews)發(fā)送hitTest:withEvent:消息,所有子視圖的遍歷順序是從top到bottom伯襟,即從subviews數(shù)組的末尾向前遍歷,直到有子視圖返回非空對象或者全部子視圖遍歷完畢猿涨;
- 若有子視圖返回非空對象,則hitTest:withEvent:方法返回此對象,處理結(jié)束(不再對其它子視圖發(fā)送hitTest:消息)姆怪;
- 如所有子視圖都返回非叛赚,則hitTest:withEvent:方法返回自身(self)。
* 來自——hitTest:withEvent:方法流程 *
最后稽揭,我們再來為其寫一個應(yīng)用實例,
Paste_Image.png
如圖:topview作為ViewController的view俺附,view1,view2都是topView的子視圖,但view1覆蓋在view2溪掀。
如果不作處理事镣,我們是沒法點擊到view2的,因為被view1遮住了揪胃,這里我們設(shè)置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 ```
結(jié)果:



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