iOS开发 - touchBegan事件判断点击的位置在View上还是在View的子View上

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

此方法用到的频率还是很高的,但是有一种情况,如下图:
这里写图片描述
同时有白色蓝色两个视图,蓝色为白色视图的子视图,两个视图都有各自点击的事件,怎么来判断我点击的是哪个,你可以使用Tap的手势来写,通过tap.view.tag来区分是哪个view,还有一种办法就是利用touch方法来解决,实现的效果如下:
这里写图片描述

来看看代码是怎么写的:

//给出两个view,不多解释了
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor=[UIColor grayColor];
    whiteView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    whiteView.center=self.view.center;
    whiteView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:whiteView];

    blueLayer=[CALayer layer];
    blueLayer.frame=CGRectMake(50, 50, 100, 100);
    blueLayer.backgroundColor=[UIColor blueColor].CGColor;
    [whiteView.layer addSublayer:blueLayer];

}
//touch的响应方法
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    //convert point to the white layer's coordinates拿到在self.view上但同时在whiteView上的点,下面的同这里一样,不一一解释了
    point = [whiteView.layer convertPoint:point fromLayer:self.view.layer]; //get layer using containsPoint:
    if ([whiteView.layer containsPoint:point]) {
        //convert point to blueLayer’s coordinates
        point = [blueLayer convertPoint:point fromLayer:whiteView.layer];
        if ([blueLayer containsPoint:point])
        {
            [[[UIAlertView alloc] initWithTitle:@"Inside Blue Layer"
                                        message:nil
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
        else
        {
            [[[UIAlertView alloc] initWithTitle:@"Inside White Layer"
                                        message:nil
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles: nil]show];
        }

    }

    //----------华丽丽的分割线,从这里开始是我写的点击的方法,相对来说比上面使用起来更方便点
//    CGPoint point=[[touches anyObject]locationInView:self.view];


//    CALayer *layer=[whiteView.layer hitTest:point];
//    if (layer==whiteView.layer) {
//        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"" message:@"Inside white layer" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
//        [alertView show];
//    }
//    else if (layer==blueLayer){
//        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"" message:@"Inside blue layer" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
//        [alertView show];
//    }
}

想下载的点击这里

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值