美文网首页iOS Developer
自定义UIWindow来实现整屏幕的弹出视图

自定义UIWindow来实现整屏幕的弹出视图

作者: 快到碗里来____ | 来源:发表于2017-03-23 16:29 被阅读253次

创建全屏幕的弹出视图

  • 自定义一个View继承与UIWindow
  • 重写init方法
static AlertWindow *singleInstance;
-(instancetype)init
{
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
        self.windowLevel = UIWindowLevelAlert - 1;  
        singleInstance = self;      
    }
    return self;
}
  • windowLevel(window层级)
//总共三种  默认为Nomal
UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;//0
UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;//2000
UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar//1000

将windowLevel设为UIWindowLevelAlert - 1确保出现在最上层

  • 设置contentView用来显示需要的内容
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width - 80, 200)];
    _contentView.backgroundColor = [UIColor whiteColor];
    [_contentView setCenter:CGPointMake(self.bounds.size.width / 2, self.bounds.size.height /2 )];
    [self addSubview:_contentView];

      UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _contentView.frame.size.width, 150)];
      label.textAlignment = NSTextAlignmentCenter;
      label.text = @"show the window";
      label.textColor = [UIColor blackColor];
      [_contentView addSubview:label];
      
      UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 149, _contentView.frame.size.width, 1)];
      line.backgroundColor = [UIColor blackColor];
      [_contentView addSubview:line];
      
      UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
      button.frame = CGRectMake(0, 150, _contentView.frame.size.width, 50);
      [button setTitle:@"Done" forState:UIControlStateNormal];
      [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
      [button addTarget:self action:@selector(hideWithAnimation) forControlEvents:UIControlEventTouchUpInside];
      [_contentView addSubview:button];
      
      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideWithAnimation)];
      [self addGestureRecognizer:tap];
      
      UITapGestureRecognizer *other = [[UITapGestureRecognizer alloc] init];
      [_contentView addGestureRecognizer:other];
    
  • 为self添加tap手势,在点击黑色背景的时候调用hide方法关闭视图
  • 为contentView添加tap手势,覆盖掉self上的手势,避免点击contentView范围调用hide方法
  • 添加show和hide方法
-(void)showWithAnimation:(BOOL)animation
{
    [self makeKeyAndVisible];
    
    [UIView animateWithDuration:animation ? 0.3 : 0
                     animations:^{
                     }
                     completion:^(BOOL finished) {
                         
                     }];
}
-(void)hideWithAnimation:(BOOL)animation
{
    [UIView animateWithDuration:animation ? 0.3 : 0
                     animations:^{
                         self.alpha = 0;
                     }
                     completion:^(BOOL finished) {
                         singleInstance = nil;
                     }];
}
  • 重写dealloc方法调用resignKeyWindow
-(void)dealloc
{
    [self resignKeyWindow];
}

运用这个方法可以实现一些自定义的弹出视图

github

Demo已上传到github 地址

相关文章

网友评论

    本文标题:自定义UIWindow来实现整屏幕的弹出视图

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