最近要做这么个效果 : 上图是行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判断点击的点是否在某区域从而实现点击不同区域实现不同方法;