首页 > 其他分享 >UI定制 - 全局提示框

UI定制 - 全局提示框

时间:2023-12-25 15:14:13浏览次数:21  
标签:self NSString cancelBtn UI frame alterView sureBtn 提示框 定制

■ 全局提示框

在开发中,一个全局提示框是很有必要的,毕竟系统的弹框是满足不了产品的变态要求。 实现这样一个功能其实很简单

代码示例: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

相关文章

  • m基于Yolov2深度学习网络的驾驶员打电话行为预警系统matlab仿真,带GUI界面
    1.算法仿真效果matlab2022a仿真结果如下: 2.算法涉及理论知识概要       随着汽车数量的不断增加,交通安全问题日益突出。其中,驾驶员手持电话行为是导致交通事故的一个重要原因。为了降低这类事故的发生率,本文提出了一种基于Yolov2深度学习网络的驾驶员手持电话行......
  • 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Progress进度条组件
    鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之Progress进度条组件编辑一、操作环境操作系统: Windows10专业版、IDE:DevEcoStudio3.1、SDK:HarmonyOS3.1二、Progress组件进度条也是UI开发最常用的组件之一,进度条组件,用于显示内容加载或操作处理等进度。接口Progress(options:{value:n......
  • blazor maui hybrid app显示本地图片
    啊......一通操作下来感觉就是两个字折磨跨平台有跨平台的好处但框架本身支持的有限很多东西做起来很曲折哎这里总结一下笔者为了折腾本地图片显示的尝试为什么要做本地图片展示呢如果是做需要网络连接的app这个一般是不需要的(要做上传前预览/编辑的话还是要的)但对......
  • python 把包含uincode字符串变成中文
    1defget_info_by_pattern(text,pattern):2p=re.compile(pattern)3p_res=p.findall(text)4returnp_res56#把包含uincode字符串变成中文7defunicode_to_chinese(text):8pattern_unicode='u[0-9a-z]{4}'9p_res=get_i......
  • Hzero教程:初始化数据库及同步表结构(基于liquibase + groovy)
    初始化数据库更新时间:2023-12-0115:38:30介绍项目创建成功之后,需要初始化本地数据库。在开发之前,请确保本地项目已经创建成功,详见创建项目创建用户确保数据库启动成功,并创建项目访问的用户。CREATEUSER'hzero'@'%'IDENTIFIEDBY"hzero";创建数据库用户创建成功之后,创建项目对......
  • Unity3D UI帧动画详解
    nity3D是一款非常强大的游戏开发引擎,它提供了丰富的功能和工具,使开发者能够轻松创建各种类型的游戏。其中,UI(UserInterface)是游戏开发中非常重要的一部分,它用于展示游戏中的各种信息和交互元素。在Unity3D中,我们可以使用UI帧动画来创建各种炫酷的UI效果。本文将详细介绍Unity3D中U......
  • 【DIY】自制STM32_Arduino
    【DIY】自制STM32_Arduino电路板前言为了在STM32上使用SimpleFOC,使用SimpleFOC的Arduino驱动板,就有了制作STM32_Arduino电路板的想法。使用STM32F103C8T6作为主控,使用立创EDA专业版软件进行电路板设计。功能需求:电路板外形上与Arduino外形相同。使用STM32的定时器TIM输出引......
  • ZIMP - swagger-ui
     zzh@ZZHPC:/zdata/Github$gitclonehttps://github.com/swagger-api/swagger-ui.gitCloninginto'swagger-ui'...remote:Enumeratingobjects:41700,done.remote:Countingobjects:100%(530/530),done.remote:Compressingobjects:100%(251/251......
  • Debian12 (雷池WAF SafeLine)通过 EUI64 固定 IPv6 地址后缀、获取无状态 SLAAC
    说明Debian12(KDE桌面)网络管理(IPv4、IPv6)已默认不再由内核直接管理,转而使用由NetworkManager管理。若要固定IPv6后缀并实现SLAAC无状态管理,则需要修改NetworkManager配置。图形界面配置网络配置完成后,检查网络地址ipaddshowenp3s0,公网和内网IPv6后缀......
  • Druid源码阅读--带流程图
    一、架构分析​Druid类图如下所示:​两大核心类:DruidDataSource和DruidAbstractDataSource​连接有效性check:从连接池中获取连接后会做有效性check,在类中有ValidConnectionChecker接口,对应有不同数据库的实现​异常处理:针对不同的数据库,druid......