CGRectContainsPoint 使用

最近要做这么个效果 : 上图是行cell . 橘黄色的是可以输入的 其他部分不可输入 ,这个表格可以上下和左右滑动。点击表格前面的名字给选中行cell加个遮罩。现在问题来了 

现在问题来了;怎么样实现 点击橘黄色部分弹出输入框 点击其他部分隐藏遮罩取消选择呢?

遮罩是cell上加个view 因为背景色不同无法通过背景色改变实现遮罩效果;

下面就需要用到:

CGRectContainsPoint

这个 就是判断点击的点是否在某一区域内:

判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数

    CGPoint point = [touch locationInView:self];

    if(CGRectContainsPoint(CGRectMake(55, 40, 30, 35), point)){

//如果这个点在CGRect里面

}

这就好办了 当点击在textfile的范围内弹出输入框。其他地方隐藏:

下面通过demo来说明:

UIView *coverV;

-(void)creatView{

    

    coverV = [[UIView alloc]init];

    [coverV  setFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];

    [coverV setBackgroundColor:[UIColor cyanColor]];

    [coverV setAlpha:0.3];

    [self.view addSubview:coverV];

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

    [coverV addGestureRecognizer:tap];    

}

-(void)tapAction:(UITapGestureRecognizer*)touch{

    

    CGPoint point = [touch locationInView:coverV];

    //如果是在cell中的点击事件 同过下面的方法可以拿到cell 和 indexPath

//     TestCell *cell = (TestCell *)tap.view.superview;

//     NSIndexPath *indexPath = [tabView indexPathForCell:cell];

    

    //如果在这个范围内 判断点击的点是否在某个范围内

    

    if(CGRectContainsPoint(CGRectMake(0, 0, 50, 50), point)){

       

        NSLog(@"点击了左上角");

    }else if(CGRectContainsPoint(CGRectMake(self.view.frame.size.width-50, 150, 50, 50), point)){

        NSLog(@"点击了右下角");

    }else{

         NSLog(@"其它");

    }

}

同一个VIew 同过点击不同的区域,通过CGRectContainsPoint判断点击的点是否在某区域从而实现点击不同区域实现不同方法;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值