■ 全局提示框
在开发中,一个全局提示框是很有必要的,毕竟系统的弹框是满足不了产品的变态要求。 实现这样一个功能其实很简单
代码示例:PopView 就是我们要封装的全局弹框
// - PopView.h
1 #import <UIKit/UIKit.h> 2 typedef void(^cancelBlock)(void); 3 typedef void(^sureBlock)(void); 4 @interface PopView : UIView 5 6 @property(nonatomic,copy)cancelBlock cancelBtnBlock; 7 @property(nonatomic,copy)sureBlock sureBtnBlock; 8 9 + (instancetype)alterViewWithTitle:(NSString *)title 10 content:(NSString *)content 11 infoSure:(NSString *)sure 12 sureBtnClicked:(sureBlock)sureBlock 13 infoCancel:(NSString *)cancle 14 cancelBtnClicked:(cancelBlock)cancleBlock; 15 @end
// - PopView.m
1 #import "PopView.h" 2 #define WIDTH_SCREEN [UIScreen mainScreen].bounds.size.width 3 #define HEIGHT_SCREENH [UIScreen mainScreen].bounds.size.height 4 @interface PopView () 5 6 @property(nonatomic,retain)UIView *alterView; // 弹窗 7 @property(nonatomic,retain)UILabel *titleLb; 8 @property(nonatomic,retain)UILabel *contentLb; 9 10 @property(nonatomic,copy)NSString *title; // 标题 11 @property(nonatomic,copy)NSString *content; // 内容 12 // 确认 13 @property(nonatomic,retain)UIButton *sureBtn; 14 @property(nonatomic,copy)NSString *sureString; 15 // 取消 16 @property(nonatomic,retain)UIButton *cancelBtn; 17 @property(nonatomic,copy)NSString *cancelString; 18 19 @end 20 21 @implementation PopView 22 - (instancetype)initWithFrame:(CGRect)frame{ 23 24 self = [super initWithFrame:frame]; 25 if(self){ 26 27 // 将遮罩层添加到 keyWindow 上 28 UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 29 // 做戏就做全套,添加一个淡黑色遮罩层:除了弹框,其他地方就不能点击 30 UIView *maskView = [[UIView alloc] init]; 31 maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; 32 maskView.frame = window.frame; 33 maskView.tag = 1001; 34 [window addSubview:maskView]; 35 36 // 可简单写一些动画 37 [UIView animateWithDuration:0.3 animations:^{ 38 maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3]; 39 }]; 40 41 42 float y_Space = 20.0; 43 float x_Space = 30.0; 44 45 // 标题 46 _titleLb = [[UILabel alloc] initWithFrame:CGRectMake(x_Space, y_Space, self.bounds.size.width/1.3, 30)]; 47 _titleLb.textColor = [UIColor colorWithRed:111/255.0 green:222/255.0 blue:199/255.0 alpha:1.0]; 48 _titleLb.font = [UIFont systemFontOfSize:18]; 49 _titleLb.adjustsFontForContentSizeCategory = YES; 50 [self addSubview:_titleLb]; 51 52 // 内容 53 _contentLb = [[UILabel alloc] initWithFrame:CGRectMake(_titleLb.frame.origin.x, CGRectGetMaxY(_titleLb.frame)+5, self.bounds.size.width/1.3, 30)]; 54 _contentLb.font = [UIFont systemFontOfSize:15]; 55 _contentLb.textColor = [UIColor darkGrayColor]; 56 [self addSubview:_contentLb]; 57 58 59 // 确定按钮 60 _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 61 _sureBtn.layer.cornerRadius = 5; 62 _sureBtn.layer.masksToBounds = YES; 63 _sureBtn.backgroundColor = _titleLb.textColor; 64 [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 65 [_sureBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; 66 [self addSubview:_sureBtn]; 67 _sureBtn.frame = CGRectMake(self.bounds.size.width/2 + x_Space, CGRectGetMaxY(self.frame)-45-y_Space, self.bounds.size.width/2 - 2*x_Space, 45); 68 69 // 取消按钮 70 _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 71 _cancelBtn.layer.cornerRadius = 5; 72 _cancelBtn.layer.masksToBounds = YES; 73 _cancelBtn.layer.borderColor = _sureBtn.backgroundColor.CGColor; 74 _cancelBtn.layer.borderWidth = 1.5; 75 [_cancelBtn setTitleColor:_sureBtn.backgroundColor forState:UIControlStateNormal]; 76 [_cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; 77 [self addSubview:_cancelBtn]; 78 _cancelBtn.frame = CGRectMake(x_Space, CGRectGetMinY(_sureBtn.frame), _sureBtn.frame.size.width, _sureBtn.frame.size.height); 79 80 // 将 self 添加进遮罩层 81 [maskView addSubview:self]; 82 } 83 return self; 84 } 85 86 #pragma mark - 封装方法 87 + (instancetype)alterViewWithTitle:(NSString *)title 88 content:(NSString *)content 89 infoSure:(NSString *)sure 90 sureBtnClicked:(sureBlock)sureBlock 91 infoCancel:(NSString *)cancle 92 cancelBtnClicked:(cancelBlock)cancleBlock{ 93 94 PopView *alterView=[[PopView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN/1.1, HEIGHT_SCREENH/3.3)]; 95 alterView.backgroundColor = [UIColor whiteColor]; 96 alterView.center = CGPointMake(WIDTH_SCREEN/2, HEIGHT_SCREENH/2 +20); 97 alterView.layer.cornerRadius = 5; 98 alterView.layer.masksToBounds = YES; 99 100 // 开始赋值 101 alterView.title = title; 102 alterView.content = content; 103 alterView.sureString = sure; 104 alterView.sureBtnBlock = sureBlock; 105 alterView.cancelString = cancle; 106 alterView.cancelBtnBlock = cancleBlock; 107 108 return alterView; 109 } 110 111 #pragma mark - 属性赋值 112 - (void)setTitle:(NSString *)title{ 113 _titleLb.text = title; 114 } 115 116 - (void)setContent:(NSString *)content{ 117 _contentLb.text = content; 118 } 119 120 - (void)setSureString:(NSString *)sureString{ 121 [_sureBtn setTitle:sureString forState:UIControlStateNormal]; 122 [_sureBtn setTitle:@" " forState:UIControlStateHighlighted]; 123 } 124 125 - (void)setCancelString:(NSString *)cancelString{ 126 127 [_cancelBtn setTitle:cancelString forState:UIControlStateNormal]; 128 [_cancelBtn setTitle:@" " forState:UIControlStateHighlighted]; 129 } 130 131 #pragma mark - 点击事件 132 - (void)btnClick:(UIButton *)btn{ 133 // 移除遮罩 134 UIView *maskView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:1001]; 135 [maskView removeFromSuperview]; 136 // 触发判断 137 if([btn isEqual:_sureBtn]){ 138 self.sureBtnBlock(); 139 }else{ 140 self.cancelBtnBlock(); 141 } 142 } 143 144 @end
// - ViewController.m
1 #import "ViewController.h" 2 #import "PopView.h" 3 @implementation ViewController 4 5 - (void)viewDidLoad { 6 [super viewDidLoad]; 7 } 8 9 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 10 [super touchesBegan:touches withEvent:event]; 11 12 [PopView alterViewWithTitle:@"系统提示" content:@"请立即重置密码" infoSure:@"确定" sureBtnClicked:^{ 13 14 } infoCancel:@"取消" cancelBtnClicked:^{ 15 16 }]; 17 } 18 19 @end
运行效果
标签:self,NSString,cancelBtn,UI,frame,alterView,sureBtn,提示框,定制 From: https://www.cnblogs.com/self-epoch/p/17926119.html