iOS开发-自定义弹窗的一种解决方式

如图所示,假如我们要实现这样的弹窗,用什么方法比较好呢?
YD20190307-172401.png
先说下之前的一种笨方法吧,在self.view的最上层先添加一个半透明的CoverView,然后再添加这块视图contentView,通过控制CoverView和ContentView的Hidden来控制是否显示

弊端:
1.会使Controller里面代码比较臃肿(当然可以通过分类,分离相关代码)
2.动画不太好做

今天要介绍的方法,是采用一个单独的ViewController来实现这个弹窗,细节代码如下
#import "XPCPopAlertController.h"

@interface XPCPopAlertController ()
@property (nonatomic, strong) UIView *content;
@end

@implementation XPCPopAlertController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // 这一步非常重要
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setviews];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 动画效果
    [UIView animateWithDuration:0.25f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn animations:^{
                            self.content.alpha = 1;
                        } completion:nil];
}
// 视图和布局
- (void)setviews {
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    
    UIView *content = [[UIView alloc] init];
    content.backgroundColor = [UIColor whiteColor];
    content.layer.cornerRadius = kSCALE(8);
    content.clipsToBounds = YES;
    content.alpha = 0;
    [self.view addSubview:content];
    _content = content;
    
    // 获取语音验证码
    UILabel *titleL = [[UILabel alloc] init];
    titleL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:kSCALE(16)];
    titleL.text = @"获取语音验证码";
    titleL.textColor = UIColor.blackColor;
    [titleL sizeToFit];
    [content addSubview:titleL];
    
    // 将电话告知您验证码,请注意接听!
    UILabel *detailL = [[UILabel alloc] init];
    detailL.font = [UIFont fontWithName:@"PingFangSC-Regular" size:kSCALE(14)];
    detailL.textColor = UIColor.blackColor;
    detailL.text = @"将电话告知您验证码,请注意接听!";
    [detailL sizeToFit];
    [content addSubview:detailL];
    
    // 横线
    UIView *line = [UIView new];
    line.backgroundColor = RGBColor(0xd9d9d9);
    [content addSubview:line];
    
    // 知道了
    UIButton *knowB = [[UIButton alloc] init];
    [knowB setTitle:@"知道了" forState:UIControlStateNormal];
    [knowB setTitleColor:RGBColor(0xff3333) forState:UIControlStateNormal];
    [knowB.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Semibold" size:kSCALE(15)]];
    [knowB addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [content addSubview:knowB];
    
    //
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kSCALE(280), kSCALE(133)));
        make.center.equalTo(self.view);
    }];
    
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kSCALE(16));
        make.top.mas_equalTo(kSCALE(24));
        make.centerX.equalTo(content);
    }];
    
    [detailL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kSCALE(14));
        make.top.mas_equalTo(titleL.mas_bottom).offset(12);
        make.centerX.equalTo(content);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kOnePx);
        make.left.right.equalTo(content);
        make.top.mas_equalTo(detailL.mas_bottom).offset(kSCALE(23));
    }];
    
    [knowB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(content);
        make.top.mas_equalTo(line.mas_bottom);
    }];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:NO completion:nil];
}

@end

弹出弹窗时,直接采用以下方法

 XPCPopAlertController *vc = [[XPCPopAlertController alloc] init];
 [self.navigationController presentViewController:vc animated:NO completion:nil];
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,802评论 1 32
  • 以下文章来自于网络论坛。然后摘抄至自己的简书。原文链接大家可以自己去网上搜。此文仅供参考。 当我们开始设计View...
    不够果断是种癌阅读 5,367评论 1 11
  • 之前听到一个很搞笑的笑话: 一个人从床上摔下来,来到了医院,医生问他:你哪里受伤了?病人回答:我也不知道,按哪里哪...
    欣欣然也阅读 2,490评论 0 3
  • 小学低年级的时候,孩子们各个都是小淘气,谁不爱玩,爱睡个懒觉,爱吃点好吃的。 对于那时的我①来说,上学实在是件...
    一乱眼繁华FL阅读 1,852评论 0 1
  • 进入初三已经快两个月了,从期初考到第一次月考,你的成绩都有了很大的进步、家长群里表扬名单里能找到你的名字了,昨天居...
    芃宝娘阅读 1,443评论 0 0