美文网首页
iOS点击空白处消失的自定义弹框

iOS点击空白处消失的自定义弹框

作者: 大风先生bigWind | 来源:发表于2017-08-30 16:39 被阅读0次

    1.概述

    APP的开发中肯定会涉及到弹框, 虽然有系统自带的弹框, 但往往是不足以应对开发中的弹框的, 所以这里就本人项目中使用到的弹框来分享一下自己的弹框设计.

    弹窗.gif

    2. 栗子

    新建一个继承自UIView的弹窗文件, 在.h文件里是这样的

    #import <UIKit/UIKit.h>
    
    @interface RSAlertView : UIView
    
    #pragma mark - 提示登录的弹框
    - (instancetype)initIfLogin;
    
    
    // 登录的block
    @property (nonatomic, copy) void(^loginBlock)();
    
    // 注册的block
    @property (nonatomic, copy) void(^registerBlock)();
    
    
    - (void)showRSAlertView;
    
    @end
    

    蓝后, 在.m文件里是这样的

    #import "RSAlertView.h"
    
    @interface RSAlertView()
    
    //弹窗
    @property (nonatomic,retain) UIView *alertView;
    
    @end
    
    @implementation RSAlertView
    
    #pragma mark - 提示登录的弹框
    - (instancetype)initIfLogin{
        
        if (self == [super init]) {
            
    //        [self removeFromSuperview];
            
            // 底面蒙版
            self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
            self.backgroundColor = [UIColor grayColor];
            
            
            // 白色弹框
            self.alertView = [[UIView alloc] init];
            self.alertView.backgroundColor = [UIColor whiteColor];
            self.alertView.layer.cornerRadius = 10;
            self.layer.masksToBounds = YES;
            self.alertView.bounds = CGRectMake(0, 0, 300, 300);
            self.alertView.center = self.center;
            [self addSubview:self.alertView];
            
            // 标题
            UILabel *titleLab = [[UILabel alloc] init];
            titleLab.font = [UIFont systemFontOfSize:16];
            titleLab.textColor = [UIColor blackColor];
            titleLab.textAlignment = NSTextAlignmentCenter;
            [self.alertView addSubview:titleLab];
            titleLab.frame = CGRectMake(0, 0, 200, 30);
            titleLab.text = @"请先登录/注册再进行操作";
    
            
            // 注册 按钮
            UIButton *registerBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, 100, 30)];
            [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
            registerBtn.backgroundColor = [UIColor blueColor];
            [registerBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            // 圆角, 边框
            registerBtn.layer.cornerRadius = 10;
            registerBtn.layer.masksToBounds = YES;
            [self.alertView addSubview:registerBtn];
    
            
            // 登录 按钮
            UIButton *loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(120, 150, 100, 30)];
            [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
            [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
            loginBtn.backgroundColor = [UIColor orangeColor];
            // 圆角, 边框
            loginBtn.layer.cornerRadius = 10;
            loginBtn.layer.masksToBounds = YES;
            [self.alertView addSubview:loginBtn];
            
            // 点击手势
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressInAlertViewGesture:)];
            [self addGestureRecognizer:tapGesture];
            
        }
        
        return self;
        
    }
    
    - (void)registerBtnClick{
    
        if (self.registerBlock) {
            self.registerBlock();
        }
        
        [self removeFromSuperview];
        
    }
    
    - (void)loginBtnClick{
        
        if (self.loginBlock) {
            self.loginBlock();
        }
        [self removeFromSuperview];
    }
    
    // 点击其他区域关闭弹窗
    - (void)handletapPressInAlertViewGesture:(UITapGestureRecognizer *)sender
    {
        if (sender.state == UIGestureRecognizerStateEnded){
            CGPoint location = [sender locationInView:nil];
            if (![_alertView pointInside:[_alertView convertPoint:location fromView:_alertView.window] withEvent:nil]){
                [_alertView.window removeGestureRecognizer:sender];
                [self removeFromSuperview];
            }
        }
    }
    
    
    #pragma mark - 弹出 -
    - (void)showRSAlertView
    {
        
         UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
        
        [rootWindow addSubview:self];
        
        [self creatShowAnimation];
    }
    
    #pragma mark - 弹出的动画
    - (void)creatShowAnimation
    {
        self.alertView.layer.position = self.center;
        self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
        [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
            self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        } completion:^(BOOL finished) {
        }];
    }
    
    
    @end
    

    3. 弹窗的使用和你可能会遇到的坑

    在你需要的页面中, 需要的地方直接创建和弹出就可以了, 例如这样的

        /*
        登录的弹框
         d*/
        RSAlertView *alerV = [[RSAlertView alloc] initIfLogin];
        alerV.registerBlock = ^{
            
            NSLog(@"注册的block");
        };
        alerV.loginBlock = ^(){
        
            NSLog(@"登录的block");
        };
        [alerV showRSAlertView];
    

    在你做测试demo的过程中, 可能你会遇到弹窗无法弹出来的情况, 这时候你到处找原因, 还可能找不到. 这个坑我也遇到, 那就是在viewDidLoad或者viewWillAppear等方法里面创建了弹框, 这时候弹框是显示不出来的.

    找原因的话你可能会发现原因是[UIApplication sharedApplication].keyWindow为nil. 为啥呢???

    关于这个问题, 也是个坑, 可以看我的另一篇文章里面, 有对这个的一些探究
    iOS [UIApplication sharedApplication].keyWindow为nil的分析

    end

    相关文章

      网友评论

          本文标题:iOS点击空白处消失的自定义弹框

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