1.概述
APP的開發(fā)中肯定會涉及到彈框, 雖然有系統(tǒng)自帶的彈框, 但往往是不足以應(yīng)對開發(fā)中的彈框的, 所以這里就本人項(xiàng)目中使用到的彈框來分享一下自己的彈框設(shè)計.
彈窗.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
藍(lán)后, 在.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];
// 標(biāo)題
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 = @"請先登錄/注冊再進(jìn)行操作";
// 注冊 按鈕
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];
// 點(diǎn)擊手勢
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];
}
// 點(diǎn)擊其他區(qū)域關(guān)閉彈窗
- (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. 彈窗的使用和你可能會遇到的坑
在你需要的頁面中, 需要的地方直接創(chuàng)建和彈出就可以了, 例如這樣的
/*
登錄的彈框
d*/
RSAlertView *alerV = [[RSAlertView alloc] initIfLogin];
alerV.registerBlock = ^{
NSLog(@"注冊的block");
};
alerV.loginBlock = ^(){
NSLog(@"登錄的block");
};
[alerV showRSAlertView];
在你做測試demo的過程中, 可能你會遇到彈窗無法彈出來的情況, 這時候你到處找原因, 還可能找不到. 這個坑我也遇到, 那就是在viewDidLoad或者viewWillAppear等方法里面創(chuàng)建了彈框, 這時候彈框是顯示不出來的.
找原因的話你可能會發(fā)現(xiàn)原因是[UIApplication sharedApplication].keyWindow為nil. 為啥呢???
關(guān)于這個問題, 也是個坑, 可以看我的另一篇文章里面, 有對這個的一些探究
iOS [UIApplication sharedApplication].keyWindow為nil的分析