首页 > 其他分享 >自定义弹框

自定义弹框

时间:2023-07-07 22:45:26浏览次数:41  
标签:confirmButton mas 自定义 make 弹框 backView void self

项目要求 就自己写了一个弹框 不知道对不对 先记录下来 目前支持 四种样式吧 可以按钮文字 内容的个别文字变色

 

 改变文字颜色的调用方法

 SJZAlertViewController *vc = [[SJZAlertViewController alloc] init];

    __weak typeof(self) weakSelf = self;

    NSString *cardStr = [NSString stringWithFormat:@"确定消耗%@张推荐卡在%@吗?",cardnumStr,cardNameStr];

    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;// 这个写里面就好了 不需要这里写

    NSMutableArray *rangeArray = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithRange:NSMakeRange(4, cardnumStr.length+1)],[NSValue valueWithRange:NSMakeRange(9+cardnumStr.length, cardNameStr.length+1)], nil];

    NSMutableArray *colorArray = [[NSMutableArray alloc] initWithObjects:RGBA(45, 217, 201, 1),RGBA(45, 217, 201, 1), nil];

    [vc alerWithButtonConfigurationTitle:@"推荐卡消耗" message:cardStr style:SJZAlertControllerStyleAlert withRange:rangeArray color:colorArray leftButton:@"取消" rightButton:@"确定" success:^{

        [weakSelf setcardNetWorking];

        } cancel:^{

            

        } orDelete:^{

            

        }];

 

    [[self viewController] presentViewController:vc animated:YES completion:^{

            

    }];

 

按钮文字变化的 

  __weak typeof(self) weakSelf = self;

    SJZAlertViewController *vc = [[SJZAlertViewController alloc] init];

    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;

    [vc alerWithButtonConfigurationTitle:@"结束直播" message:@"您正在直播中,确定结束直播并退出吗?" style:SJZAlertControllerStyleDelete withRange:@[] color:@[] leftButton:@"结束直播" rightButton:@"最小化" success:^{

            

        } cancel:^{

            [weakSelf closeRoom];

        } orDelete:^{

            

        }];

    [self presentViewController:vc animated:YES completion:^{

            

    }];

其他 看下面 .h .m 文件

 

 

后续再加功能

.h 文件

#import <UIKit/UIKit.h>

 

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, SJZAlertControllerStyle) {

    /** 双按钮 */

    SJZAlertControllerStyleAlert,

    /** 底部弹框 */

    SJZAlertControllerStyleActionSheet,

    /** 单按钮 */

    SJZAlertControllerStyleOnly,

    /** 双按钮右上侧带删除 */

    SJZAlertControllerStyleDelete,

    /** 单按钮 右上带删除*/

    SJZAlertControllerStyleOnlyORDelete,

    

};

 

 

 

@interface SJZAlertViewController : UIViewController

 

/** 可配置弹框 (目前单按钮 只传右按钮文字)

 title 标题

 message 内容

 style 类型

 rangeArray 内容富文本 range 他有几个 colorArray 就有几个 后期优化

 leftStr 左按钮文字 (单按钮不传)

 cancel 左按钮点击事件

 rightStr 右按钮文字

 success 右点击事件

 deletea 删除按钮点击事件

 */

- (void)alerWithButtonConfigurationTitle:(NSString *)title message:(NSString *)message style:(SJZAlertControllerStyle)style withRange:(NSMutableArray *)rangeArray color:(NSMutableArray *)colorArray leftButton:(NSString *)leftStr rightButton:(NSString *)rightStr success:(void (^)(void))success cancel:(void(^)(void))cancel orDelete:(void(^)(void))deletea;

 

- (void)alerSheetTitle:(NSArray *)titleArray style:(SJZAlertControllerStyle)style dataBlock:(void(^)(NSString *titleStr))succes;

@end

 

.m  文件

 

#import "SJZAlertViewController.h"

 

@interface SJZAlertViewController ()

 

@property (nonatomic ,strong) UIView *backView;

@property (nonatomic ,strong) UIButton *deleteButton;

@property (nonatomic ,strong) UILabel *titleLabel;

@property (nonatomic ,strong) UILabel *contentLabel;

@property (nonatomic ,strong) UIButton *cancelButton;

@property (nonatomic ,strong) UIButton *confirmButton;

@property (nonatomic ,copy) void (^confirmBlock)(void);

@property (nonatomic ,copy) void (^cancelBlock)(void);

@property (nonatomic ,copy) void (^deleteBlock)(void);

 

@property (nonatomic ,copy) void (^sheetBlock)(NSString *titleStr);

 

@end

 

@implementation SJZAlertViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];

}

 

-(void)alerWithButtonConfigurationTitle:(NSString *)title message:(NSString *)message style:(SJZAlertControllerStyle)style withRange:(NSMutableArray *)rangeArray color:(NSMutableArray *)colorArray leftButton:(NSString *)leftStr rightButton:(NSString *)rightStr success:(void (^)(void))success cancel:(void (^)(void))cancel orDelete:(void (^)(void))deletea {

    

    CGSize size = [SJZTools addwithNstring:message orTextSize:15 andTypeface:@"" isbold:NO maxfloat:KWidth-120 fontweight:UIFontWeightRegular];

    

    float height = size.height> 40 ? size.height+10 : 40;

    

    [self.view addSubview:self.backView];

    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(35);

        make.right.mas_equalTo(-35);

        make.centerY.equalTo(self.view.mas_centerY).offset(0);

        make.height.mas_equalTo(130+height);

    }];

    _backView.layer.cornerRadius = 20;

    _backView.layer.masksToBounds = YES;

    [self.backView addSubview:self.titleLabel];

    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(45);

        make.right.mas_offset(-45);

        make.top.mas_equalTo(24);

        make.height.mas_equalTo(20);

    }];

    [self.backView addSubview:self.deleteButton];

    [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_offset(-10);

        make.top.mas_equalTo(10);

        make.height.width.mas_offset(26);

    }];

    [self.backView addSubview:self.contentLabel];

    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(24);

        make.right.mas_equalTo(-24);

        make.top.equalTo(_titleLabel.mas_bottom).offset(15);

        make.height.mas_equalTo(height);

    }];

    [self.backView addSubview:self.cancelButton];

    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(24);

        make.bottom.mas_equalTo(-20);

        make.width.mas_equalTo((KWidth-120)/2-7.5);

        make.height.mas_equalTo(42);

        

    }];

    [self.backView addSubview:self.confirmButton];

    

    

    if (style == SJZAlertControllerStyleAlert) {

        _deleteButton.hidden = YES;

        _cancelButton.hidden = _confirmButton.hidden = NO;

        self.confirmButton.frame = CGRectMake(KWidth-35-24-(KWidth-120)/2-7.5-15, height+130-42-20, (KWidth-120)/2-7.5, 42);

    } else if (style == SJZAlertControllerStyleOnly) {

        _cancelButton.hidden = _deleteButton.hidden = YES;

        _confirmButton.hidden = NO;

        self.confirmButton.frame = CGRectMake(24, height+130-42-20, KWidth-120, 42);

        

    } else if (style == SJZAlertControllerStyleDelete){

        _deleteButton.hidden = _cancelButton.hidden = _confirmButton.hidden = NO;

        self.confirmButton.frame = CGRectMake(KWidth-35-24-(KWidth-120)/2-7.5-15, height+130-42-20, (KWidth-120)/2-7.5, 42);

    } else if (style == SJZAlertControllerStyleOnlyORDelete) {

        _cancelButton.hidden  = YES;

        _confirmButton.hidden = _deleteButton.hidden = NO;

        self.confirmButton.frame = CGRectMake(KWidth-35-24-(KWidth-120)/2-7.5-15, height+130-42-20, (KWidth-120)/2-7.5, 42);

    }

    

    if (height > 40) {

        _contentLabel.textAlignment = NSTextAlignmentLeft;

    } else {

        _contentLabel.textAlignment = NSTextAlignmentCenter;

    }

    

    _titleLabel.text = title;

    if (rangeArray.count != 0) {

        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:message];

        

        for (int i = 0; i < rangeArray.count; i ++) {

            NSValue *value = rangeArray[i];

            [attStr addAttribute:NSForegroundColorAttributeName value:colorArray[i] range:value.rangeValue];

        }

        

        _contentLabel.attributedText = attStr;

        

    } else {

        _contentLabel.text = message;

    }

    

    [_cancelButton setTitle:leftStr forState:UIControlStateNormal];

    [_confirmButton setTitle:rightStr forState:UIControlStateNormal];

    

    self.confirmBlock = ^{

        if(success) {

            success();

        }

    };

    self.cancelBlock = ^{

        if (cancel) {

            cancel();

        }

    };

    self.deleteBlock = ^{

        if (deletea) {

            deletea();

        }

    };

    

    CAGradientLayer * gradientLayer = [CAGradientLayer layer];

    gradientLayer.frame = _confirmButton.bounds;

    //    设置渐变颜色数组

    gradientLayer.colors = @[(__bridge id)RGB(45, 217, 201).CGColor,(__bridge id)RGB(153, 247, 186).CGColor];

    //    渐变颜色的区间分布

    gradientLayer.locations = @[@0,@0.8];

    //    起始位置

    gradientLayer.startPoint = CGPointMake(0, 0);

    //    结束位置

    gradientLayer.endPoint = CGPointMake(1, 0);

    gradientLayer.cornerRadius = 21;

    [_confirmButton.layer addSublayer:gradientLayer];

    _confirmButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightHeavy];

    

    

}

 

- (void)alerSheetTitle:(NSArray *)titleArray style:(SJZAlertControllerStyle)style dataBlock:(void (^)(NSString * _Nonnull))succes {

    

    self.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;

    [self.view addSubview:self.backView];

    self.backView.frame = CGRectMake(0, KHeight-titleArray.count*50-kTabbarSafeBottomMargin, KWidth, titleArray.count*50+kTabbarSafeBottomMargin);

    

    UIRectCorner corner = UIRectCornerTopRight | UIRectCornerTopLeft;

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_backView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(20, 20)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

    maskLayer.frame = _backView.bounds;

    maskLayer.path = path.CGPath;

    _backView.layer.masksToBounds = YES;

    _backView.layer.mask = maskLayer;

    

    for (int i = 0; i < titleArray.count; i ++) {

        UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 50*i, KWidth, 50)];

        [titleButton setTitle:titleArray[i] forState:UIControlStateNormal];

        [titleButton setTitleColor:BLACKCOLOR forState:UIControlStateNormal];

        titleButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];

        [titleButton addTarget:self action:@selector(ClickTitleButton:) forControlEvents:UIControlEventTouchUpInside];

        [_backView addSubview:titleButton];

    }

    self.sheetBlock = ^(NSString *titleStr) {

        if (succes) {

            succes(titleStr);

        }

    };

    

}

- (void)ClickTitleButton:(UIButton *)btn {

    if (self.sheetBlock) {

        self.sheetBlock(btn.titleLabel.text);

    }

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}

 

- (void)selectClickConfirm:(UIButton *)btn {

    if (self.confirmBlock) {

        self.confirmBlock();

    }

    

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}

- (void)selectClickCancel:(UIButton *)btn {

    if (self.cancelBlock) {

        self.cancelBlock();

    }

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}

- (void)selectClickDelete:(UIButton *)btn {

    if (self.deleteBlock) {

        self.deleteBlock();

    }

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

}

#pragma mark ----- UI

- (UIView *)backView {

    if (!_backView) {

        _backView = [[UIView alloc] init];

        _backView.backgroundColor = [UIColor whiteColor];

    

    }

    return _backView;

}

 

- (UILabel *)titleLabel {

    if (!_titleLabel) {

        _titleLabel = [[UILabel alloc] init];

        _titleLabel.textColor = BLACKCOLOR;

        _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];

        _titleLabel.textAlignment = NSTextAlignmentCenter;

    }

    return _titleLabel;

}

 

- (UIButton *)deleteButton {

    if (!_deleteButton) {

        _deleteButton = [[UIButton alloc] init];

        [_deleteButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];

        [_deleteButton addTarget:self action:@selector(selectClickDelete:) forControlEvents:UIControlEventTouchUpInside];

    }

    return _deleteButton;

}

- (UILabel *)contentLabel {

    if (!_contentLabel) {

        _contentLabel = [[UILabel alloc] init];

        _contentLabel.textColor = BLACKCOLOR;

        _contentLabel.textAlignment = NSTextAlignmentCenter;

        _contentLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];

        _contentLabel.numberOfLines = 0;

    }

    return _contentLabel;

}

- (UIButton *)cancelButton {

    if (!_cancelButton) {

        _cancelButton = [[UIButton alloc] init];

        [_cancelButton setBackgroundColor:RGBA(244, 247, 247, 1)];

        _cancelButton.layer.masksToBounds = YES;

        _cancelButton.layer.cornerRadius = 21;

        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];

        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightHeavy];

        [_cancelButton addTarget:self action:@selector(selectClickCancel:) forControlEvents:UIControlEventTouchUpInside];

    }

    return _cancelButton;

}

- (UIButton *)confirmButton {

    if (!_confirmButton) {

        _confirmButton = [[UIButton alloc] init];

        _confirmButton.layer.masksToBounds = YES;

        _confirmButton.layer.cornerRadius = 21;

        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];

        [_confirmButton setTitleColor:BLACKCOLOR forState:UIControlStateNormal];

        [_confirmButton addTarget:self action:@selector(selectClickConfirm:) forControlEvents:UIControlEventTouchUpInside];

    }

    return _confirmButton;

}

 

/*

 #pragma mark - Navigation

 

 // In a storyboard-based application, you will often want to do a little preparation before navigation

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

 // Get the new view controller using [segue destinationViewController].

 // Pass the selected object to the new view controller.

 }

 */

 

@end

 

 

 

 

 

标签:confirmButton,mas,自定义,make,弹框,backView,void,self
From: https://www.cnblogs.com/xiaodeng90/p/17536233.html

相关文章

  • uvicorn自定义日志
    BOSS问为什么对FastAPI的请求会有非200,一直抛异常,定位问题ing。在对请求的处理日志中没有找到对应记录,请求没有过来。观察uvicorn的日志,默认没有请求时间,无法比对。INFO:127.0.0.1:7225-"POST/appHTTP/1.1"200OK现在给这个日志加时间:importasyncioimportim......
  • 自定义事件内容分发
    这个听的云里雾里,下面是一个示例demo<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><divid="app"><todo>&l......
  • C#自定义checkBox开关按钮控件,设计漂亮美观的UI按钮
    第一步:先准备开关按钮要使用到的背景图片,一张是开启的,一张是关闭的,如下图: 一共有6种款式,大家也可以全部加进去    然后将这些图片作为资源文件添加到项目中,如下图: 第二步、新建用户控件,命名为:ButtonCheck.cs ButtonCheck.cs代码如下:///<su......
  • vue 自定义指令实现overflow-tooltip
    利用elementui el-tooltip组件实现1.创建overflow-tooltip指令importVuefrom'vue'/*eslint-disable*/functiongetStyle(obj,attr){if(obj.currentStyle){returnobj.currentStyle[attr];}else{returngetComputedStyle(obj)[attr];......
  • 手把手教你自定义自己SpringBoot Starter组件源码剖析
    我们知道SpringBootStarter也就是启动器。是SpringBoot组件化的一大优点。基于这个思想,基于这个思想SpringBoot才变得非常强大,官方给我们提供很多开箱即用的启动器。SpringBootStarter是SpringBoot的一个重要特性,它有以下优点:依赖管理:Starter自动处理项目的依赖关系,......
  • 前端Vue自定义精美底部操作栏导航栏工具栏 可用于电商购物车底部导航
    随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随意的进行组合。大大提升开发效率......
  • uniapp实现这该死的自定义弹窗
    最近自己学习和写这个uniapp,刚开始规划的时候就觉得自己到时候会需要一个这个弹框,弹框里面药房input这样的东西什么的,然后就在想uniapp里面会不会没有这个modal呢?转念一想应该是会有的吧,毕竟是一个框架嘛.然后我就找找找,找了好久都没找到合适的.可能是人家有我没找到吧,最......
  • Django restframwork中使用分页及实现自定义分页
    关于为何要分页以及如何在Django+Template架构中如何使用分页,可以参考之前的文章django自定义分页类和使用总结[1]DjangoRestFramework中分页限制今天开篇我们先不讲如何使用,我们先说Django+restframework实现前后端分离项目开发时,分页功能使用的限制?缘由是之前在开发运维......
  • Vue 先初始化子组件再初始化父组件的方法(自定义父子组件mounted执行顺序)
    写在前面:本篇内容内容主要讲述了,在使用Konva进行开发过程中遇到的一些问题。(既然是组件加载顺序,主要牵扯到的就是,父子组件的关系,父子组件的生命周期)众所周知,Vue中父子组件生命周期的执行顺序为://挂载阶段父beforeCreate->父created->父beforeMount->子beforeCre......
  • ERP导出表格自定义格式R报表开发
    按照正常流程新建程序,画面修改上传,程序下载修改导入JAVA包,在global.import下 IMPORTcomIMPORTJAVAjava.net.URLIMPORTJAVAorg.apache.poi.ss.util.CellRangeAddressIMPORTJAVAorg.apache.poi.ss.util.RegionUtilIMPORTJAVAjava.io.InputStreamIMPORTJAVAjava......