在平时的开发中经常会用到遮罩层,类似于你在点击退出的时候屏幕上只能点击的有“确定”和“取消”,而后面有一层黑色的视图,点击没有任何响应。黑色的视图就是一个遮罩层(我叫它遮罩层)。这个可以用来做退出按钮,做网络加载是的视图。废话不多说,下面是我写的一个关于遮罩层的应用,先看效果,然后在贴代码

ios开发 添加遮盖 ios 遮罩_#import


代码如下:

首先我在故事板中创建了一个点击按钮,然后将按钮拖到ViewController中(事件)。

ios开发 添加遮盖 ios 遮罩_#pragma_02

代码我都写了注释,有什么不明白的可以看注释

1.类结构图

ios开发 添加遮盖 ios 遮罩_遮罩层_03

2.代码

ZQView.h中的代码


#import <UIKit/UIKit.h>

@interface ZQView : UIView
@property (nonatomic, strong)UIView *BacgroundView;//遮罩层
@property (nonatomic, strong)UITableView *tableView;//表格视图

@end


ZQView.m


#import "ZQView.h"
@interface ZQView()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_dataSource;// 数据源
    NSIndexPath *_indexPath;// 行数(这个是为了对选择某个单元格退出之后再加载表格出现表格还是选择某个单元格的问题)
}
@end

@implementation ZQView

#pragma mark *** System Method ***


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化数据源
        _dataSource = @[@"不限",@"锦江区",@"青阳区",@"金牛区",@"武侯区",@"成华区"];
        // 添加视图
        [self addSubview:self.BacgroundView];
        [self addSubview:self.tableView];
    }
    return self;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // 判断是否有点击的indexPath
    if (_indexPath) {
        // 当移除表格视图之前先将单元格的accessoryType置为没有
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:_indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
        // 重新加载一下这个单元格
        [self.tableView reloadRowsAtIndexPaths:@[_indexPath] withRowAnimation:UITableViewRowAnimationNone];
        // 将表格视图移除父视图
    }
    [self removeFromSuperview];
}
#pragma mark *** UITableViewDelegate ***
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 点击某个单元格将单元格的accessoryType置为“√”
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    // 记录选中的单元格
    _indexPath = indexPath;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 点击某个单元格将单元格的accessoryType置为没有
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}
#pragma mark *** UITableViewDataSource ***
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 返回数据源的个数
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"zhaoqian";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    // 设置单元格文字显示
    cell.textLabel.text = [_dataSource objectAtIndex:indexPath.row];
    return cell;
}

// 这里是我自己加的头不视图,其实加不加无所谓,看各位的喜好
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    // 设置单元格头部视图
    
    // 表格头部最底层的视图
    UIView *BACView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 60)];
    // 设置显示的文字
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, self.bounds.size.width - 20 , 60)];
    // 设置颜色
    BACView.backgroundColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor whiteColor];
    // 设置文字内容
    label.text = @"行政区域";
    // 设置label的颜色
    label.textColor = [UIColor colorWithRed:126.0/255.0 green:93.0/255.0 blue:191.0/255.0 alpha:1.0];
    // 设置字体的大小
    label.font = [UIFont systemFontOfSize:20];
    // 设置下面的分割线
    UIView *LineView = [[UIView alloc]initWithFrame:CGRectMake(0, 58, self.bounds.size.width, 2)];
    LineView.backgroundColor = [UIColor colorWithRed:126.0/255.0 green:93.0/255.0 blue:191.0/255.0 alpha:1.0];
    // 将label和分割线加到表格最底层视图上
    [BACView addSubview:label];
    [BACView addSubview:LineView];
    // 返回这个底层视图
    return BACView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    // 设置表格头部视图的高度
    return 60;
}
#pragma mark *** Lazy Loading ***
// 设置遮罩层
- (UIView *)BacgroundView{
    if (!_BacgroundView) {
        _BacgroundView = [[UIView alloc]initWithFrame:self.bounds];
        // 这个语句:遮罩层设置透明度之后,不会影响遮罩层上面的视图的透明度
        _BacgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    return _BacgroundView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMidY(self.frame), self.bounds.size.width, self.bounds.size.height/2) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}



@end



ViewController.h中的代码

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end


ViewController.m中的代码


#import "ViewController.h"
#import "ZQView.h"

@interface ViewController ()
@property (nonatomic ,strong)ZQView *zqView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
// 这个是在StroyBoard中的button
- (IBAction)PopViewButton:(UIButton *)sender {
    // 添加遮罩的视图
    [self.view addSubview:self.zqView];
}

#pragma mark *** Lazy Loading ***
- (ZQView *)zqView{
    if (!_zqView) {
        _zqView = [[ZQView alloc]initWithFrame:self.view.bounds];
    }
    return _zqView;
}

@end