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

 

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

标签: ios
  1035人阅读  评论(0)  收藏  举报
  分类:

其实很简单,直接上代码

[objc]  view plain  copy
  1. #import <UIKit/UIKit.h>  
  2. // .h文件  
  3. @interface ShowAnimationView : UIView  
  4.   
  5. -(void)showView;  
  6.   
  7. @end  
  8.   
  9. // .m文件  
  10. #import "ShowAnimationView.h"  
  11. @interface ShowAnimationView ()  
  12.   
  13. @property (nonatomicstrong) UIView  *contentView;  
  14. @end  
  15.   
  16. @implementation ShowAnimationView  
  17.   
  18. - (instancetype)initWithFrame:(CGRect)frame  
  19. {  
  20.     self = [super initWithFrame:frame];  
  21.     if (self) {  
  22.         [self layoutAllSubviews];  
  23.     }  
  24.     return self;  
  25. }  
  26.   
  27. - (void)layoutAllSubviews{  
  28.       
  29.     /*创建灰色背景*/  
  30.     UIView *bgView = [[UIView alloc] initWithFrame:self.frame];  
  31.     bgView.alpha = 0.3;  
  32.     bgView.backgroundColor = [UIColor blackColor];  
  33.     [self addSubview:bgView];  
  34.       
  35.       
  36.     /*添加手势事件,移除View*/  
  37.     UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];  
  38.     [bgView addGestureRecognizer:tapGesture];  
  39.       
  40.     /*创建显示View*/  
  41.     _contentView = [[UIView alloc] init];  
  42.     _contentView.frame = CGRectMake(00self.frame.size.width - 40180);  
  43.     _contentView.backgroundColor=[UIColor whiteColor];  
  44.     _contentView.layer.cornerRadius = 4;  
  45.     _contentView.layer.masksToBounds = YES;  
  46.     [self addSubview:_contentView];  
  47.   /*可以继续在其中添加一些View 虾米的*/  
  48.       
  49. }  
  50. #pragma mark - 手势点击事件,移除View  
  51. - (void)dismissContactView:(UITapGestureRecognizer *)tapGesture{  
  52.       
  53.     [self dismissContactView];  
  54. }  
  55.   
  56. -(void)dismissContactView  
  57. {  
  58.     __weak typeof(self)weakSelf = self;  
  59.     [UIView animateWithDuration:0.5 animations:^{  
  60.         weakSelf.alpha = 0;  
  61.     } completion:^(BOOL finished) {  
  62.         [weakSelf removeFromSuperview];  
  63.     }];  
  64.       
  65. }  
  66.   
  67. // 这里加载在了window上  
  68. -(void)showView  
  69. {  
  70.     UIWindow * window = [UIApplication sharedApplication].windows[0];  
  71.     [window addSubview:self];  
  72. }  
  73.   
  74. /* 
  75. // Only override drawRect: if you perform custom drawing. 
  76. // An empty implementation adversely affects performance during animation. 
  77. - (void)drawRect:(CGRect)rect { 
  78.     // Drawing code 
  79. } 
  80. */  
  81.   
  82. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值