iOS 自定义AlertController 分享

GitHub地址 CYAlertController

CYAlertController


一个初学者的第一次尝试。喜欢的,支持的,欢迎 star⭐️,fork。没有很多经验,很多东西都是自己的思路,如果有不对的地方希望大家能够指出。

高仿官方 Alert 外观,提供多种自定义的转场动画,以及相似的 API。

外观&动画


支持多个button,自适应高度

部分动画






使用


导入

使用 cocoapods

pod 'CYAlertController'

也可以直接往项目中拖入 CYAlertController 文件夹到你的项目中

然后在需要使用的地方导入头文件

#import "CYAlertController.h"

调用

CYAlertController 提供了几乎与官方一样的API:

CYAlertController *alert = [CYAlertController alertWithTitle:@"警告!警告!💥"
                                                     message:@"逗你玩儿呢 ~ 😜"];
alert.presentStyle = CYAlertPresentStyleSystem;
alert.dismissStyle = CYAlertDismissStyleFadeOut;

// 或者直接一口气初始化
CYAlertController *alert2 = [CYAlertController alertWithTitle:@"警告!警告!💥"
                                                      message:@"逗你玩儿呢 ~ 😜"
                                                 presentStyle:CYAlertPresentStyleSystem
                                                 dismissStyle:CYAlertDismissStyleFadeOut];
                                                 
// 还可以设置 alertView 的圆角半径,默认为6
alert.alertViewCornerRadius = 10;

创建 action(提供了3中与官方一样的style),给 alert 添加 action :

CYAlertAction *defaultAction = [CYAlertAction actionWithTitle:@"确定" style:CYAlertActionStyleDefault handler:^{ NSLog(@"Default"); }];
CYAlertAction *destructiveAction = [CYAlertAction actionWithTitle:@"危险" style:CYAlertActionStyleDestructive handler:^{ NSLog(@"Destructive"); }];
CYAlertAction *cancelAction = [CYAlertAction actionWithTitle:@"取消" style:CYAlertActionStyleCancel handler:^{ NSLog(@"Cancel"); }];

// 一次性添加
[alert addActions:@[defaultAction, destructiveAction, cancelAction]];
    
// 也可以一个个添加
[alert addAction:defaultAction];
[alert addAction:destructiveAction];
[alert addAction:cancelAction];

最后就直接 present:

[self presentViewController:alert animated:YES completion:nil];

转场的动画

目前提供了9种present动画,7种dismiss动画

// present style
typedef NS_ENUM(NSInteger, CYAlertPresentStyle) {
    CYAlertPresentStyleSystem,          // 系统样式
    CYAlertPresentStyleFadeIn,          // 渐入
    CYAlertPresentStyleBounce,          // 弹出
    CYAlertPresentStyleExpandHorizontal,// 水平展开
    CYAlertPresentStyleExpandVertical,  // 垂直展开
    CYAlertPresentStyleSlideDown,       // 从上往下划入
    CYAlertPresentStyleSlideUp,         // 从下往上划入
    CYAlertPresentStyleSlideLeft,       // 从右往左划入
    CYAlertPresentStyleSlideRight,      // 从左往右划入
};

// dismiss style
typedef NS_ENUM(NSInteger, CYAlertDismissStyle) {
    CYAlertDismissStyleFadeOut,             // 渐出
    CYAlertDismissStyleContractHorizontal,  // 水平收起
    CYAlertDismissStyleContractVertical,    // 垂直收起
    CYAlertDismissStyleSlideDown,           // 向下划出
    CYAlertDismissStyleSlideUp,             // 向上划出
    CYAlertDismissStyleSlideLeft,           // 向左划出
    CYAlertDismissStyleSlideRight,          // 向右划出
};

更多细节可以直接看demo~

计划


  • 加入更多的转场动画
  • 支持自定义动画
  • 提供更多种类的alert,如 ActionSheet
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标...
    VincentHK阅读 5,440评论 3 44
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • 1.画好底稿,用浅棕色和绿色混合使用,涂花的枝干,哪个先涂都可以。 再用墨绿色把花的叶子进行勾边。 2.用墨绿色对...
    梨梧桐阅读 927评论 0 6
  • 日子已经过去大半,从写作开篇到现在,有不少提升。不会盲目去为了完成打卡任务而任意写作,不会随意去挑好完成的...
    vivian2阅读 113评论 0 0