在自定义View中 设置背景蒙版
bgView = [[UIImageView alloc] initWithFrame:UIScreen.mainScreen.bounds];
bgView.image = [UIImage imageWithColor:RGB_A(17, 17, 17, .5)];
bgView.userInteractionEnabled = YES;
bgView.tag = 1;
[[UIApplication sharedApplication].keyWindow addSubview:bgView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgAction)];
tap.delegate=self;
[bgView addGestureRecognizer:tap];
UIView *contentView = [[UIView alloc]initWithFrame:lxy(Width(16), KScreenH/4, KScreenW-Width(32), KScreenH/2)];
contentView.backgroundColor = Underline;
[bgView addSubview:contentView];
如上述代码 bgView为背景蒙版层 添加手势 添加手势代理UIGestureRecognizerDelegate 代理方法设置如下
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
// NSLog(@"aaaa=%@",NSStringFromClass([touch.view class]) );
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UIImageView"]) {
return YES;
}
return NO;
}
当除了bgView是imageView类型外还有一个或多个imageView 那么代理方法可以如下设置
#pragma mark 点击背景移除view
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UIImageView"]) {
if (touch.view.tag==1) {
return YES;
}else{
return NO;
}
}
return NO;
}
效果图如下 截屏的图有些大 x的按钮背景是为了展示按钮点击范围 上篇文章有介绍