響應者鏈詳解——尋找被觸摸的view

尋找被觸摸的View.jpg

在iOS系統(tǒng)中,當用戶觸摸了一個view后,一個完整的事件響應是分為兩個過程的:

  1. 尋找被觸摸的view;
  2. 處理觸摸事件;

在本篇文章中我要介紹的是第一個過程。
每個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 ```

結果:

![點擊view2區(qū)域](http://upload-images.jianshu.io/upload_images/2069613-80b1c8c213d8e395.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![點擊view1的其它區(qū)域](http://upload-images.jianshu.io/upload_images/2069613-e31b9211b6ff46ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


![點擊topView](http://upload-images.jianshu.io/upload_images/2069613-68797ee2640182a8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



參考:[hitTest:withEvent:方法流程](http://blog.csdn.net/jiajiayouba/article/details/23447145)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容