iOS全屏遮罩简单实现
1.首先创建一个SYMaskView.h 和.m文件 ,在SYMaskView.h 中公开两个方法
//
// SYMaskView.h
// Created by wenbin lu on 2018/8/7.
// Copyright © 2018年 sinkon. All rights reserved.
//
#import
@interfaceSYMaskView :UIView
+ (void)showMaskComplement:(void(^)(SYMaskView*maskView))complement;
+ (void)hideMaskComplement:(void(^)(SYMaskView*maskView))complement;
2.在 SYMaskView.m中实现两个类方法
//单列模式
+ (instancetype)shareView{
staticdispatch_once_tonceToken;
static SYMaskView*sharedView;
dispatch_once(&onceToken, ^{
sharedView = [[self alloc] initWithFrame:[[[UIApplication sharedApplication] delegate] window].bounds];
//设置半透明
sharedView.backgroundColor = [UIColor colorWithRGB:0x000000 alpha:0.5];
//添加到window上
[[UIApplication sharedApplication].keyWindow addSubview:sharedView];
});
returnsharedView;
}
//显示遮罩
+ (void)showMaskComplement:(void(^)(SYMaskView*maskView))complement{
[[selfshareView]setHidden:NO];
complement([selfshareView]);
}
//隐藏遮罩
+ (void)hideMaskComplement:(void(^)(SYMaskView*maskView))complement{
[[selfshareView]setHidden:YES];
complement([selfshareView]);
}
//隐藏遮罩
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
[selfsetHidden:YES];
}
3.在需要使用的地方直接调用
//通过block将遮罩对象传出来,可以实现在遮罩上添加子控件
[SYMaskView showMaskComplement:^(SYMaskView *maskView) {
NSLog(@"试图%@",maskView);
weakSelf.ShowSpecificationsBlock(maskView);
}];