美文网首页
自定义一个全屏的蒙板(导航栏也能遮挡住)

自定义一个全屏的蒙板(导航栏也能遮挡住)

作者: LeeLeCoder | 来源:发表于2017-04-18 10:48 被阅读0次
#import <UIKit/UIKit.h>
// .h文件
@interface ShowAnimationView : UIView

-(void)showView;

@end

// .m文件
#import "ShowAnimationView.h"
@interface ShowAnimationView ()

@property (nonatomic, strong) UIView  *contentView;
@end

@implementation ShowAnimationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutAllSubviews];
    }
    return self;
}

- (void)layoutAllSubviews{  
      
    /*创建灰色背景*/  
    UIView *bgView = [[UIView alloc] initWithFrame:self.frame];  
    bgView.alpha = 0.3;  
    bgView.backgroundColor = [UIColor blackColor];  
    [self addSubview:bgView];  
      
      
    /*添加手势事件,移除View*/  
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];  
    [bgView addGestureRecognizer:tapGesture];  
      
    /*创建显示View*/  
    _contentView = [[UIView alloc] init];  
    _contentView.frame = CGRectMake(0, 0, self.frame.size.width - 40, 180);  
    _contentView.backgroundColor=[UIColor whiteColor];  
    _contentView.layer.cornerRadius = 4;  
    _contentView.layer.masksToBounds = YES;  
    [self addSubview:_contentView];  
  /*可以继续在其中添加一些View 虾米的*/  
      
}  
#pragma mark - 手势点击事件,移除View  
- (void)dismissContactView:(UITapGestureRecognizer *)tapGesture{  
      
    [self dismissContactView];  
}  
  
-(void)dismissContactView  
{  
    __weak typeof(self)weakSelf = self;  
    [UIView animateWithDuration:0.5 animations:^{  
        weakSelf.alpha = 0;  
    } completion:^(BOOL finished) {  
        [weakSelf removeFromSuperview];  
    }];  
      
}  
  
// 这里加载在了window上  
-(void)showView  
{  
    UIWindow * window = [UIApplication sharedApplication].windows[0];  
    [window addSubview:self];  
}  
@end   

如果项目中多处使用到了蒙版功能,可以将蒙版单独拿出来,子视图继承该蒙版视图。代码如下所示:

#import <UIKit/UIKit.h>

@interface LLBaseCardView : UIView

/**
 显示小卡片
 */
- (void)showView;

@end


#import "LLBaseCardView.h"

#define LLRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
/** 卡片距离左边和右边的间距 */
#define cardLeftRightMargin 30
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation LLBaseCardView

// 这里加载在了window上
- (void)showView
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self layoutAllSubviews];
    }
    return self;
}

- (void)layoutAllSubviews{
    
    // 创建灰色背景
    UIView *bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    bgView.backgroundColor = LLRGBAColor(59, 59, 59, 0.5);
    bgView.userInteractionEnabled = YES;
    [self addSubview:bgView];
    
    // 添加手势事件,移除View
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__dismissContactView:)];
    [bgView addGestureRecognizer:tapGesture];
}

#pragma mark - 手势点击事件,移除View
- (void)__dismissContactView:(UITapGestureRecognizer *)tapGesture{
    __weak typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

创建继承于LLBaseCardView的子视图

#import "LLBaseCardView.h"

@interface LLCard : LLBaseCardView

@end


#import "LLCard.h"

@implementation LLCard

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        [self initSubviews];
    }
    return self;
}

#pragma mark - 初始化子控件
- (void)initSubviews
{
    // 内容面板
    CGFloat contentViewW = kScreenWidth - cardLeftRightMargin*2;
    CGFloat contentViewH = (5.0/8.0)*contentViewW;
    CGFloat contentViewY = (kScreenHeight - contentViewH)*0.5;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(cardLeftRightMargin, contentViewY, contentViewW, contentViewH)];
    contentView.backgroundColor = [UIColor whiteColor];
    [contentView roundCornerWithRadius:10];
    [self addSubview:contentView];
    
   // add subviews in here
}

@end

相关文章

网友评论

      本文标题:自定义一个全屏的蒙板(导航栏也能遮挡住)

      本文链接:https://www.haomeiwen.com/subject/mtjfzttx.html