iOSer 干货部落iOS CodingiOS程序猿

响应者链详解——寻找被触摸的view

2016-07-08  本文已影响361人  此心不改
寻找被触摸的View.jpg

在iOS系统中,当用户触摸了一个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。

如果用户点击了view0(触摸点不在view1或view2上)。

view找到了,我们来作个总结:

  • 首先调用当前视图的pointInside: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区域](http:https://img.haomeiwen.com/i2069613/80b1c8c213d8e395.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![点击view1的其它区域](http:https://img.haomeiwen.com/i2069613/e31b9211b6ff46ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


![点击topView](http:https://img.haomeiwen.com/i2069613/68797ee2640182a8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)



参考:[hitTest:withEvent:方法流程](http://blog.csdn.net/jiajiayouba/article/details/23447145)
上一篇 下一篇

猜你喜欢

热点阅读