判断点击view是否是自己想要的view的三种方法

1>监听touchBengan方法

- (void)viewDidLoad {
    [super viewDidLoad];
    _redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    _redView.backgroundColor = [UIColor redColor];
    _redView.center = self.view.center;
    [self.view addSubview:_redView];
    
    _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _blackView.backgroundColor = [UIColor blackColor];
    [_redView addSubview:_blackView];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self.view];
    point = [_redView.layer convertPoint:point fromLayer:self.view.layer];
    if ([_redView.layer containsPoint:point]) {
        point = [_blackView.layer convertPoint:point fromLayer:_redView.layer];
        if ([_blackView.layer containsPoint:point]) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"点击了黑色" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertVC addAction:action];
            [self presentViewController:alertVC animated:YES completion:nil];
        }
    }
    
    if ([_redView.layer containsPoint:point]) {
        point = [_blackView.layer convertPoint:point fromLayer:_redView.layer];
        if (![_blackView.layer containsPoint:point]) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"点击了红色" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertVC addAction:action];
            [self presentViewController:alertVC animated:YES completion:nil];
        }
    }
    
}

效果如下:

判断点击view是否是自己想要的view的三种方法_第1张图片

2>设置tag值,添加tap手势,在tap手势的监听方法中判断点击view的tag值是否是自己想要点击的view

- (void)viewDidLoad {
    [super viewDidLoad];
    _redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    _redView.backgroundColor = [UIColor redColor];
    _redView.center = self.view.center;
    _redView.tag = 100;
    [_redView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]];
    [self.view addSubview:_redView];
    
    _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _blackView.backgroundColor = [UIColor blackColor];
    _blackView.tag = 101;
    [_blackView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]];
    [_redView addSubview:_blackView];
}

- (void)tapAction:(UITapGestureRecognizer *)tapGesture {
    if (tapGesture.view.tag == 100) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"点击了红色" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
    if (tapGesture.view.tag == 101) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"点击了黑色" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}
 效果如下: 
  
判断点击view是否是自己想要的view的三种方法_第2张图片

3>CGpath绘制点击范围

第三种方法,就是绘制一个矩形区域,判断点击的点是否在矩形区域内。

具体的实现方法可参考:http://blog.csdn.net/sky_2016/article/details/41943517

你可能感兴趣的:(iOS,iOS)