iOS Developer

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

2017-03-23  本文已影响253人  快到碗里来____

创建全屏幕的弹出视图

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;
}
//总共三种  默认为Nomal
UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;//0
UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;//2000
UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar//1000

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

  • 为self添加tap手势,在点击黑色背景的时候调用hide方法关闭视图
  • 为contentView添加tap手势,覆盖掉self上的手势,避免点击contentView范围调用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;
                     }];
}
-(void)dealloc
{
    [self resignKeyWindow];
}

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

github

Demo已上传到github 地址

上一篇 下一篇

猜你喜欢

热点阅读