CYAlertController
一個初學者的第一次嘗試。喜歡的,支援的,歡迎 star⭐️,fork。沒有很多經驗,很多東西都是自己的思路,如果有不對的地方希望大家能夠指出。
高仿官方 Alert 外觀,提供多種自定義的轉場動畫,以及相似的 API。
外觀&動畫
支援多個button,自適應高度
部分動畫
使用
匯入
使用 cocoapods
1 |
pod 'CYAlertController' |
也可以直接往專案中拖入 CYAlertController
資料夾到你的專案中
然後在需要使用的地方匯入標頭檔案
1 |
#import "CYAlertController.h" |
呼叫
CYAlertController 提供了幾乎與官方一樣的API:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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 :
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 |
[self presentViewController:alert animated:YES completion:nil]; |
轉場的動畫
目前提供了9種present動畫,7種dismiss動畫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// 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