ios 自定义对话框 实现教程
【摘要】 前言:各位同学大家好, 有段时间没有更新博客了哈 具体多久我也不记得了哈。 最近疫情严重 大家要做好防护,照顾好自己 再来看我的文章 希望我的经历能鼓舞到每一个在正在奋斗中年轻的程序员 ,废话不多说,今天要分享的是 iOS 自定义弹窗 我们正式开始 效果图这边实现使用纯代码实现哈,没有使用xib 哈 创建Popupview 继承Uiview //// PopupView.h// Po...
前言:
各位同学大家好, 有段时间没有更新博客了哈 具体多久我也不记得了哈。 最近疫情严重 大家要做好防护,照顾好自己 再来看我的文章 希望我的经历能鼓舞到每一个在正在奋斗中年轻的程序员 ,废话不多说,今天要分享的是 iOS 自定义弹窗 我们正式开始
效果图
这边实现使用纯代码实现哈,没有使用xib 哈
创建Popupview 继承Uiview
//
// PopupView.h
// PopupViewDemo
//
// Created by mac on 2022/5/12.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void (^CancelClick)(void);
typedef void (^ConfirmClick)(void);
@interface PopupView : UIView
@property (nonatomic, strong)UIColor *cancelColor; // 取消按钮的颜色
@property (nonatomic, strong)UIColor *confirmColor; // 确认按钮的颜色
@property (nonatomic, strong)UIColor *titleColor; // 标题颜色
@property (nonatomic, strong)UIFont *cancelFont; // 取消按钮的字体
@property (nonatomic, strong)UIFont *confirmFont; // 确认按钮的字体
@property (nonatomic, strong)UIFont *titleFont; // 标题的字体
/**
* AlertView初始化
*
* @param title 标题
* @param cancelTitle 取消标题
* @param confirmTitle 确认标题
* @param cancelClick 取消按钮点击事件
* @param confirmClick 确认按钮点击事件
*
* @return self
*/
- (instancetype)initWithTitle:(NSString *)title
cancel:(NSString *)cancelTitle
confirm:(NSString *)confirmTitle
cancelClick:(CancelClick)cancelClick
confirmClick:(ConfirmClick)confirmClick;
/**
* AlertView显示
*/
- (void)show;
@end
NS_ASSUME_NONNULL_END
我们定义了弹窗 initWithTitle 方法里面传入 显示的一些 文字和 和点击之后的回调, 然后定义额一些UIcolor 颜色和UIfont 字体
- (instancetype)initWithTitle:(NSString *)title cancel:(NSString *)cancelTitle confirm:(NSString *)confirmTitle cancelClick:(CancelClick)cancelClick confirmClick:(ConfirmClick)confirmClick {
if (self = [super init]) {
self.titleString = title;
self.cancelString = cancelTitle;
self.confirmString = confirmTitle;
self.cancelClick = cancelClick;
self.confirmClick = confirmClick;
//初始化子视图
}
return self;
}
初始化子视图
创建弹窗布局
- (void)installSubViews {
self.frame = [UIScreen mainScreen].bounds;
// 初始化遮罩视图
self.blackBgV = [[UIView alloc]initWithFrame:self.bounds];
self.blackBgV.backgroundColor = [UIColor grayColor];
self.blackBgV.alpha = 0;
[self addSubview:self.blackBgV];
// 初始化actionSheetView
self.alertV = [[UIView alloc] initWithFrame:CGRectMake(35,self.bounds.size.height/2-70, self.bounds.size.width - 70, 140)];
self.alertV.backgroundColor = [UIColor whiteColor];
self.alertV.layer.cornerRadius = 5;
self.alertV.layer.masksToBounds = YES;
self.alertV.alpha = 0;
[self addSubview:self.alertV];
UILabel *headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, self.alertV.bounds.size.width, 25)];
if (self.titleString && self.titleString.length>0) {
headLabel.text = self.titleString;
}
if (self.titleColor) {
headLabel.textColor = self.titleColor;
} else {
headLabel.textColor = [UIColor blackColor];
}
headLabel.textAlignment = NSTextAlignmentCenter;
if (self.titleFont) {
headLabel.font = self.titleFont;
} else {
headLabel.font = [UIFont systemFontOfSize:15];
}
[self.alertV addSubview:headLabel];
UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, headLabel.frame.size.height+headLabel.frame.origin.y+40, self.alertV.bounds.size.width, LineWidth)];
line.backgroundColor = [UIColor colorWithRed:0xdc/256.0 green:0xdc/256.0 blue:0xdc/256.0 alpha:1];
[self.alertV addSubview:line];
UILabel *line0 = [[UILabel alloc] initWithFrame:CGRectMake(self.alertV.frame.size.width/2, line.frame.origin.y+LineWidth, LineWidth, self.alertV.frame.size.height-line.frame.origin.y-1)];
line0.backgroundColor = [UIColor colorWithRed:0xdc/256.0 green:0xdc/256.0 blue:0xdc/256.0 alpha:1];
[self.alertV addSubview:line0];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
cancelButton.frame = CGRectMake(0, line.frame.origin.y+LineWidth, line0.frame.origin.x, line0.frame.size.height);
if (self.cancelFont) {
[cancelButton.titleLabel setFont:self.cancelFont];
} else {
[cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
}
if (self.cancelString) {
[cancelButton setTitle:self.cancelString forState:UIControlStateNormal];
} else {
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
}
if (self.cancelColor) {
[cancelButton setTitleColor:self.cancelColor forState:UIControlStateNormal];
} else {
[cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
[cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.alertV addSubview:cancelButton];
UIButton *confimButton = [UIButton buttonWithType:UIButtonTypeSystem];
confimButton.frame = CGRectMake(line0.frame.origin.x+LineWidth, line.frame.origin.y+LineWidth, line0.frame.origin.x, line0.frame.size.height);
if (self.confirmFont) {
[confimButton.titleLabel setFont:self.confirmFont];
} else {
[confimButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
}
if (self.confirmString) {
[confimButton setTitle:self.confirmString forState:UIControlStateNormal];
} else {
[confimButton setTitle:@"确认" forState:UIControlStateNormal];
}
if (self.confirmColor) {
[confimButton setTitleColor:self.confirmColor forState:UIControlStateNormal];
} else {
[confimButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
[confimButton addTarget:self action:@selector(confimButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.alertV addSubview:confimButton];
// 遮罩加上手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
[self.blackBgV addGestureRecognizer:tap];
self.hidden = YES;
}
// 颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
显示弹窗
- (void)show {
[self installSubViews];
[[UIApplication sharedApplication].keyWindow addSubview:self];
self.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
self.alertV.alpha = 1;
self.blackBgV.alpha = 0.5;
} completion:^(BOOL finished) {
}];
}
关闭隐藏弹窗
- (void)hidden {
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3 animations:^{
self.alertV.alpha = 0;
self.blackBgV.alpha = 0;
} completion:^(BOOL finished) {
weakSelf.hidden = YES;
[weakSelf removeFromSuperview];
}];
}
弹窗内部 按钮点击事件
- (void)confimButtonClick:(UIButton *)sender {
if (self.confirmClick) {
self.confirmClick();
}
[self hidden];
}
- (void)cancelButtonClick:(UIButton *)sender {
if(self.cancelClick) {
self.cancelClick();
}
[self hidden];
}
遮罩视图点击
#pragma mark -- 遮罩视图点击
- (void)tapClick:(UIGestureRecognizer *)tap {
NSLog(@"遮罩视图点击");
}
具体调用
- (void)buttonClick:(UIButton *)sender {
PopupView *alertView = [[PopupView alloc] initWithTitle:@"这是标题" cancel:@"取消" confirm:@"确认" cancelClick:^{
NSLog(@"取消按钮点击了");
} confirmClick:^{
NSLog(@"确认按钮点击了");
}];
alertView.titleColor = [UIColor redColor];
alertView.titleFont = [UIFont systemFontOfSize:18];
[alertView show];
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)