iOS—(XMCustomAlertView)自定义弹框控件

简介

在iOS项目迭代过程中我们经常会增加新的功能,然后还需要有新功能引导。实际情况是我们常常会看见系统的UIAlertView覆盖在引导页面上,给用户体验增加一层蒙阴。更极端的情况是产品常常希望弹框还需要分优先级依次弹出,或者弹框只能在特定页面出现。于是就有了XMCustomAlertView的诞生。

地址

github地址

功能

  • UIAlertView功能
  • 优先级
  • 有效期
  • 界面自由组合
  • 自定义动画效果
  • 带有页面属性的AlertView

依赖的环境

  • iOS 7.0 +
  • CAAnimationBlocks 0.0.1
  • RBBAnimation 0.3.0
  • TTTAttributedLabel 2.0.0

使用方法

  1. 头文件
#import "XMCustomAlertView_Define.h"
  1. 便利方法
//代理模式
XMCustomAlertView *alertView = [[XMCustomAlertView alloc] initWithTitle:@"自定义弹框" message:@"我是一个通过便利方法生成的自定义弹框控件" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
//代码块模式
XMCustomAlertView *alertView = [[XMCustomAlertView alloc] initWithTitle:@"自定义弹框" message:@"我是一个通过便利方法生成的自定义弹框控件" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] clickHandler:^(XMCustomAlertView * _Nonnull alertView, NSInteger buttonIndex) {
        NSLog(@"弹框被点击");
}];
[alertView show];
便利方法
  1. 修改标题、内容、按钮
XMCustomAlertView *alertView = [[XMCustomAlertView alloc] initWithTitle:@"自定义弹框" message:@"我是一个通过便利方法生成的自定义弹框控件" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView setTitle:@"修改标题"];
[alertView setMessage:@"修改自定义弹框内容"];
[alertView setButtonInfoWithIndex:1 blcok:^(XMAlertViewButtonBuilder * _Nonnull buttonBuilder) {
    buttonBuilder.buttoncolor = [UIColor redColor];
}];
[alertView show];
修改弹框内容
  1. UI界面自由组合
    • UI界面自由组合需要界面控件库的支持,目前库中有标题、正文、按钮、合同、滑动块控件
XMCustomAlertView *alertView = [[XMCustomAlertView alloc] initWithConfigurationHandler:nil];
XMCustomAlertViewCustomTitle *customtTitle = [XMCustomAlertViewCustomTitle customTitleWithConfigurationHandler:^(XMAlertViewTitleBuilder * _Nonnull titleBuilder) {
    titleBuilder.title = @"合同项目";
    titleBuilder.titleColor = [UIColor redColor];
}];
XMCustomAlertViewCustomMessage *customMessage = [XMCustomAlertViewCustomMessage customMessageWithConfigurationHandler:^(XMAlertViewMessageBuilder * _Nonnull messageBuilder) {
    messageBuilder.message = @"一部法律根据《立法法》第六十一条的规定,一般由编、章、节、条、款、项、目组成。编、章、节是对法条的归类。所以,在使用法律时只需引用到条、款、项、目即可,无需指出该条所在的编、章、节。";
}];
XMCustomAlertViewCustomContract *customContract = [XMCustomAlertViewCustomContract contractWithConfigurationHandler:^(XMAlertViewContractBuilder * _Nonnull contractBuilder) {
    XMAlertViewContractModel *contractModel1 = [XMAlertViewContractModel contractModelWithText:@"勾选及代表您同意" linkable:NO action:nil];
    XMAlertViewContractModel *contractModel2 = [XMAlertViewContractModel contractModelWithText:@"《立法法》" linkable:YES action:^(XMAlertViewContractModel *contractModel) {
        NSLog(@"条款被点击");
    }];
    XMAlertViewContractModel *contractModel3 = [XMAlertViewContractModel contractModelWithText:@"相关条款" linkable:NO action:nil];
    contractBuilder.contractModelArray = @[contractModel1, contractModel2, contractModel3];
    contractBuilder.checkable = YES;
} checkboxAction:^(BOOL agreeContract) {
        NSLog(@"同意/拒绝合同");
}];
XMCustomAlertViewAction *action = [XMCustomAlertViewAction actionWithConfigurationHandler:^(XMAlertViewButtonBuilder * _Nonnull buttonBuilder) {
    buttonBuilder.buttonTitle = @"取消";
} clickHander:^(XMCustomAlertViewAction * _Nonnull action) {
    NSLog(@"取消按钮被点击");
}];
[alertView addCustomViewWithControl:customtTitle];
[alertView addCustomViewWithControl:customMessage];
[alertView addCustomViewWithControl:customContract];
[alertView addAction:action];
[alertView show];
界面元素自由组合
  1. 自定义动画效果
    • 目前可以自动弹框入场和出场动画,库中有淡入淡出、左右飞入飞出、上下飞入飞出。当然,有兴趣可以自己扩展
XMCustomAlertView *alertView1 = [[XMCustomAlertView alloc] initWithTitle:@"动画" message:@"我是个实现了系统动画的弹框,可以淡入淡出" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
[alertView1 modifyConfigurationContextWithHandler:^(XMAlertViewConfigurationContext * _Nonnull configurationContext) {
    configurationContext.showAnimation = [[XMAlertViewSystemAnimationFactory new] showAnimation];
    configurationContext.dismissAnimation = [[XMAlertViewSystemAnimationFactory new] dismissAnimation];
}];
[alertView1 show];
XMCustomAlertView *alertView2 = [[XMCustomAlertView alloc] initWithTitle:@"动画" message:@"我是个从右->左出现,从左->右消失的弹框" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
[alertView2 modifyConfigurationContextWithHandler:^(XMAlertViewConfigurationContext * _Nonnull configurationContext) {
    configurationContext.showAnimation = [[XMAlertViewRightToLeftAnimationFactory new] showAnimation];
    configurationContext.dismissAnimation = [[XMAlertViewRightToLeftAnimationFactory new] dismissAnimation];
}];
[alertView2 show];
XMCustomAlertView *alertView3 = [[XMCustomAlertView alloc] initWithTitle:@"动画" message:@"我是个从下->上出现,从上->下消失的弹框" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
[alertView3 modifyConfigurationContextWithHandler:^(XMAlertViewConfigurationContext * _Nonnull configurationContext) {
    configurationContext.showAnimation = [[XMAlertViewDownToUpAnimationFactory new] showAnimation];
    configurationContext.dismissAnimation = [[XMAlertViewDownToUpAnimationFactory new] dismissAnimation];
}];
[alertView3 show];
自定义弹框动画
  1. 具有页面属性的弹框
    • 这种弹框只能在指定的页面弹出,必要时会跟着页面的消失而消失
///在页面显示时调用
[XMCustomAlertView xm_showMessageWithPageIdentifier:@"pageIdentifier" delegateObject:self];
///在页面即将消失时调用
[XMCustomAlertView xm_dismissShowingAlertViewWithPageIdentifier:@"pageIdentifier"];
///其他页面可以设置"pageIdentifier"页面属性的弹框
XMCustomAlertView *alertView = [[XMCustomAlertView alloc] initWithTitle:@"页面弹框" message:@"这是个只能在指定页面弹出的弹框" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
[alertView xm_pushMessageWithPageIdentifier:@"pageIdentifier"];
  1. 其他
  • 指定弹框的父视图
[alertView showInSuperView:viewA];
  • 弹框上显示Toast提示
[alertView showAutoDismissTips:@"Toast提示" delayTime:0.5f];
  • 弹框可以设置是否跟着屏幕一起旋转
alertView.autoRotate = YES;
  • 当然你也可以完全定制你私人的UI界面
[alertView addCustomViewWithView:CustomView];
  • 还有一些唯一性判断逻辑,此处不再赘述。

作者

主页:https://www.jianshu.com/u/0bf8dc16b794
邮箱:fang.x.m@qq.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容