如图所示,假如我们要实现这样的弹窗,用什么方法比较好呢?
先说下之前的一种笨方法吧,在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];