├── CKCustomAlertViewDemo.xcodeproj ├── xcuserdata │ └── chen.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ ├── xcschememanagement.plist │ │ └── CKCustomAlertViewDemo.xcscheme ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── chen.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── project.pbxproj ├── CKCustomAlertViewDemo ├── ViewController.h ├── AppDelegate.h ├── main.m ├── CKAlertViewController.h ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── AppDelegate.m ├── ViewController.m └── CKAlertViewController.m └── README.md /CKCustomAlertViewDemo.xcodeproj/xcuserdata/chen.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo.xcodeproj/project.xcworkspace/xcuserdata/chen.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pr-Chen/CKAlertViewController/HEAD/CKCustomAlertViewDemo.xcodeproj/project.xcworkspace/xcuserdata/chen.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // CKCustomAlertViewDemo 4 | // 5 | // Created by 陈凯 on 16/9/7. 6 | // Copyright © 2016年 陈凯. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // CKCustomAlertViewDemo 4 | // 5 | // Created by 陈凯 on 16/9/7. 6 | // Copyright © 2016年 陈凯. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // CKCustomAlertViewDemo 4 | // 5 | // Created by 陈凯 on 16/9/7. 6 | // Copyright © 2016年 陈凯. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo.xcodeproj/xcuserdata/chen.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | CKCustomAlertViewDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 7437521E1D7FEF3000BD2415 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/CKAlertViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // CKAlertViewController.h 3 | // 自定义警告框 4 | // 5 | // Created by 陈凯 on 16/8/24. 6 | // Copyright © 2016年 陈凯. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface CKAlertAction : NSObject 12 | 13 | @property (nonatomic, readonly) NSString *title; 14 | 15 | + (instancetype)actionWithTitle:(NSString *)title handler:(void (^)(CKAlertAction *action))handler; 16 | 17 | @end 18 | 19 | 20 | @interface CKAlertViewController : UIViewController 21 | 22 | @property (nonatomic, readonly) NSArray *actions; 23 | @property (nonatomic, copy) NSString *title; 24 | @property (nonatomic, copy) NSString *message; 25 | @property (nonatomic, assign) NSTextAlignment messageAlignment; 26 | 27 | + (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message; 28 | - (void)addAction:(CKAlertAction *)action; 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 一款漂亮简洁的警告框 2 | ![](https://ww4.sinaimg.cn/large/006tNbRwly1fda10j9vhbg30850egwsj.gif) 3 | ![](https://ww1.sinaimg.cn/large/006tNbRwly1fda10qv95qg30850egqjl.gif) 4 | ![](https://ww4.sinaimg.cn/large/006tNbRwly1fda135viqlg30820ebwuq.gif) 5 | 6 | ## 用法与UIAlertController类似 7 | ```objective-c 8 | CKAlertViewController *alertVC = [CKAlertViewController alertControllerWithTitle:@"Access Microphone?" message:@"Are you sure that you want to allow this app to access your microphone?" ]; 9 | 10 | CKAlertAction *cancel = [CKAlertAction actionWithTitle:@"取消" handler:^(CKAlertAction *action) { 11 | NSLog(@"点击了 %@ 按钮",action.title); 12 | }]; 13 | 14 | CKAlertAction *sure = [CKAlertAction actionWithTitle:@"确定" handler:^(CKAlertAction *action) { 15 | NSLog(@"点击了 %@ 按钮",action.title); 16 | }]; 17 | 18 | [alertVC addAction:cancel]; 19 | [alertVC addAction:sure]; 20 | 21 | [self presentViewController:alertVC animated:NO completion:nil]; 22 | ``` 23 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // CKCustomAlertViewDemo 4 | // 5 | // Created by 陈凯 on 16/9/7. 6 | // Copyright © 2016年 陈凯. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo.xcodeproj/xcuserdata/chen.xcuserdatad/xcschemes/CKCustomAlertViewDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // CKCustomAlertViewDemo 4 | // 5 | // Created by 陈凯 on 16/9/7. 6 | // Copyright © 2016年 陈凯. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "CKAlertViewController.h" 11 | 12 | @interface ViewController () 13 | 14 | @property (weak, nonatomic) IBOutlet UIButton *button1; 15 | @property (weak, nonatomic) IBOutlet UIButton *button2; 16 | @property (weak, nonatomic) IBOutlet UIButton *button3; 17 | 18 | @end 19 | 20 | @implementation ViewController 21 | 22 | - (BOOL)prefersStatusBarHidden { 23 | return YES; 24 | } 25 | 26 | - (void)viewDidLoad { 27 | [super viewDidLoad]; 28 | 29 | self.button1.layer.cornerRadius = 22.5; 30 | self.button1.layer.borderColor = [UIColor colorWithRed:1.00 green:0.03 blue:0.29 alpha:1.00].CGColor; 31 | self.button1.layer.borderWidth = 1; 32 | 33 | self.button2.layer.cornerRadius = 22.5; 34 | self.button2.layer.borderColor = [UIColor colorWithRed:0.235 green:0.709 blue:1 alpha:1.00].CGColor; 35 | self.button2.layer.borderWidth = 1; 36 | 37 | self.button3.layer.cornerRadius = 22.5; 38 | self.button3.layer.borderColor = [UIColor colorWithRed:0.15 green:0.68 blue:0.38 alpha:1.00].CGColor; 39 | self.button3.layer.borderWidth = 1; 40 | 41 | } 42 | 43 | - (IBAction)clickShowAlertButton1:(UIButton *)sender { 44 | 45 | CKAlertViewController *alertVC = [CKAlertViewController alertControllerWithTitle:@"Access Microphone?" message:@"Are you sure that you want to allow this app to access your microphone?" ]; 46 | 47 | CKAlertAction *cancel = [CKAlertAction actionWithTitle:@"取消" handler:^(CKAlertAction *action) { 48 | NSLog(@"点击了 %@ 按钮",action.title); 49 | }]; 50 | 51 | CKAlertAction *sure = [CKAlertAction actionWithTitle:@"确定" handler:^(CKAlertAction *action) { 52 | NSLog(@"点击了 %@ 按钮",action.title); 53 | }]; 54 | 55 | [alertVC addAction:cancel]; 56 | [alertVC addAction:sure]; 57 | 58 | [self presentViewController:alertVC animated:NO completion:nil]; 59 | } 60 | 61 | - (IBAction)clickShowAlertButton2:(UIButton *)sender { 62 | 63 | CKAlertViewController *alertVC = [CKAlertViewController alertControllerWithTitle:@"系统更新" message:@"iOS88.88现已准备好了, 解决了好几万个重大bug,是否更新?" ]; 64 | 65 | CKAlertAction *cancel = [CKAlertAction actionWithTitle:@"不要更新" handler:^(CKAlertAction *action) { 66 | NSLog(@"点击了 %@ 按钮",action.title); 67 | }]; 68 | 69 | CKAlertAction *updateNow = [CKAlertAction actionWithTitle:@"立即更新" handler:^(CKAlertAction *action) { 70 | NSLog(@"点击了 %@ 按钮",action.title); 71 | }]; 72 | 73 | CKAlertAction *updateLater = [CKAlertAction actionWithTitle:@"稍后更新" handler:^(CKAlertAction *action) { 74 | NSLog(@"点击了 %@ 按钮",action.title); 75 | }]; 76 | 77 | [alertVC addAction:cancel]; 78 | [alertVC addAction:updateNow]; 79 | [alertVC addAction:updateLater]; 80 | 81 | [self presentViewController:alertVC animated:NO completion:nil]; 82 | } 83 | 84 | - (IBAction)clickShowAlertButton3:(UIButton *)sender { 85 | 86 | CKAlertViewController *alertVC = [CKAlertViewController alertControllerWithTitle:@"发现新版本" message:@"1. 日夜赶工,修复了一堆bug.\n2. 跟着产品经理改来改去,增加了很多功能.\n3. 貌似性能提升了那么一点点." ]; 87 | alertVC.messageAlignment = NSTextAlignmentLeft; 88 | 89 | CKAlertAction *cancel = [CKAlertAction actionWithTitle:@"我知道了" handler:^(CKAlertAction *action) { 90 | NSLog(@"点击了 %@ 按钮",action.title); 91 | }]; 92 | 93 | CKAlertAction *update = [CKAlertAction actionWithTitle:@"立即更新" handler:^(CKAlertAction *action) { 94 | NSLog(@"点击了 %@ 按钮",action.title); 95 | }]; 96 | 97 | [alertVC addAction:cancel]; 98 | [alertVC addAction:update]; 99 | 100 | [self presentViewController:alertVC animated:NO completion:nil]; 101 | } 102 | 103 | @end 104 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 37 | 50 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo/CKAlertViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // CKAlertViewController.m 3 | // 自定义警告框 4 | // 5 | // Created by 陈凯 on 16/8/24. 6 | // Copyright © 2016年 陈凯. All rights reserved. 7 | // 8 | 9 | #import "CKAlertViewController.h" 10 | 11 | @interface CKHighLightButton : UIButton 12 | 13 | @property (strong, nonatomic) UIColor *highlightedColor; 14 | 15 | @end 16 | 17 | @implementation CKHighLightButton 18 | 19 | - (void)setHighlighted:(BOOL)highlighted { 20 | [super setHighlighted:highlighted]; 21 | 22 | if (highlighted) { 23 | self.backgroundColor = self.highlightedColor; 24 | } 25 | else { 26 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 27 | self.backgroundColor = nil; 28 | }); 29 | } 30 | } 31 | 32 | @end 33 | 34 | #define kThemeColor [UIColor colorWithRed:94/255.0 green:96/255.0 blue:102/255.0 alpha:1] 35 | 36 | @interface CKAlertAction () 37 | 38 | @property (copy, nonatomic) void(^actionHandler)(CKAlertAction *action); 39 | 40 | @end 41 | 42 | @implementation CKAlertAction 43 | 44 | + (instancetype)actionWithTitle:(NSString *)title handler:(void (^)(CKAlertAction *action))handler { 45 | CKAlertAction *instance = [CKAlertAction new]; 46 | instance -> _title = title; 47 | instance.actionHandler = handler; 48 | return instance; 49 | } 50 | 51 | @end 52 | 53 | 54 | @interface CKAlertViewController () 55 | { 56 | UIView *_shadowView; 57 | UIView *_contentView; 58 | 59 | UIEdgeInsets _contentMargin; 60 | CGFloat _contentViewWidth; 61 | CGFloat _buttonHeight; 62 | 63 | BOOL _firstDisplay; 64 | } 65 | 66 | @property (strong, nonatomic) UILabel *titleLabel; 67 | @property (strong, nonatomic) UILabel *messageLabel; 68 | @property (strong, nonatomic) NSMutableArray *mutableActions; 69 | @end 70 | 71 | @implementation CKAlertViewController 72 | 73 | + (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message { 74 | 75 | CKAlertViewController *instance = [CKAlertViewController new]; 76 | instance.title = title; 77 | instance.message = message; 78 | return instance; 79 | } 80 | 81 | - (instancetype)init { 82 | if (self = [super init]) { 83 | self.modalPresentationStyle = UIModalPresentationCustom; 84 | [self defaultSetting]; 85 | } 86 | return self; 87 | } 88 | 89 | - (void)viewDidLoad { 90 | [super viewDidLoad]; 91 | 92 | //创建对话框 93 | [self creatShadowView]; 94 | [self creatContentView]; 95 | 96 | [self creatAllButtons]; 97 | [self creatAllSeparatorLine]; 98 | 99 | self.titleLabel.text = self.title; 100 | self.messageLabel.text = self.message; 101 | } 102 | 103 | - (void)viewDidLayoutSubviews { 104 | [super viewDidLayoutSubviews]; 105 | 106 | self.view.backgroundColor = [UIColor clearColor]; 107 | 108 | //更新标题的frame 109 | [self updateTitleLabelFrame]; 110 | 111 | //更新message的frame 112 | [self updateMessageLabelFrame]; 113 | 114 | //更新按钮的frame 115 | [self updateAllButtonsFrame]; 116 | 117 | //更新分割线的frame 118 | [self updateAllSeparatorLineFrame]; 119 | 120 | //更新弹出框的frame 121 | [self updateShadowAndContentViewFrame]; 122 | 123 | //显示弹出动画 124 | [self showAppearAnimation]; 125 | } 126 | 127 | - (void)defaultSetting { 128 | 129 | _contentMargin = UIEdgeInsetsMake(25, 20, 0, 20); 130 | _contentViewWidth = 285; 131 | _buttonHeight = 45; 132 | _firstDisplay = YES; 133 | _messageAlignment = NSTextAlignmentCenter; 134 | } 135 | 136 | #pragma mark - 创建内部视图 137 | 138 | //阴影层 139 | - (void)creatShadowView { 140 | _shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _contentViewWidth, 175)]; 141 | _shadowView.layer.masksToBounds = NO; 142 | _shadowView.layer.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.25].CGColor; 143 | _shadowView.layer.shadowRadius = 20; 144 | _shadowView.layer.shadowOpacity = 1; 145 | _shadowView.layer.shadowOffset = CGSizeMake(0, 10); 146 | [self.view addSubview:_shadowView]; 147 | } 148 | 149 | //内容层 150 | - (void)creatContentView { 151 | _contentView = [[UIView alloc] initWithFrame:_shadowView.bounds]; 152 | _contentView.backgroundColor = [UIColor colorWithRed:250 green:251 blue:252 alpha:1]; 153 | _contentView.layer.cornerRadius = 13; 154 | _contentView.clipsToBounds = YES; 155 | [_shadowView addSubview:_contentView]; 156 | } 157 | 158 | //创建所有按钮 159 | - (void)creatAllButtons { 160 | 161 | for (int i=0; i2 ? self.actions.count : 1; 183 | linesAmount -= (self.title.length || self.message.length) ? 0 : 1; 184 | 185 | for (int i=0; i2 ? _contentViewWidth : _contentViewWidth/self.actions.count; 227 | 228 | for (int i=0; i2 ? 0 : buttonWidth*i; 231 | CGFloat buttonY = self.actions.count>2 ? firstButtonY+_buttonHeight*i : firstButtonY; 232 | 233 | btn.frame = CGRectMake(buttonX, buttonY, buttonWidth, _buttonHeight); 234 | } 235 | } 236 | 237 | - (void)updateAllSeparatorLineFrame { 238 | 239 | //分割线的条数 240 | NSInteger linesAmount = self.actions.count>2 ? self.actions.count : 1; 241 | linesAmount -= (self.title.length || self.message.length) ? 0 : 1; 242 | NSInteger offsetAmount = (self.title.length || self.message.length) ? 0 : 1; 243 | for (int i=0; i0 ? 15 : 0; 290 | return firstButtonY; 291 | } 292 | 293 | #pragma mark - 事件响应 294 | - (void)didClickButton:(UIButton *)sender { 295 | CKAlertAction *action = self.actions[sender.tag-10]; 296 | if (action.actionHandler) { 297 | action.actionHandler(action); 298 | } 299 | 300 | [self showDisappearAnimation]; 301 | } 302 | 303 | #pragma mark - 其他方法 304 | 305 | - (void)addAction:(CKAlertAction *)action { 306 | [self.mutableActions addObject:action]; 307 | } 308 | 309 | - (UILabel *)creatLabelWithFontSize:(CGFloat)fontSize { 310 | 311 | UILabel *label = [UILabel new]; 312 | label.numberOfLines = 0; 313 | label.font = [UIFont systemFontOfSize:fontSize]; 314 | label.textColor = kThemeColor; 315 | return label; 316 | } 317 | 318 | - (void)showAppearAnimation { 319 | 320 | if (_firstDisplay) { 321 | _firstDisplay = NO; 322 | _shadowView.alpha = 0; 323 | _shadowView.transform = CGAffineTransformMakeScale(1.1, 1.1); 324 | [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.55 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseIn animations:^{ 325 | _shadowView.transform = CGAffineTransformIdentity; 326 | _shadowView.alpha = 1; 327 | } completion:nil]; 328 | } 329 | } 330 | 331 | - (void)showDisappearAnimation { 332 | 333 | [UIView animateWithDuration:0.1 animations:^{ 334 | _contentView.alpha = 0; 335 | } completion:^(BOOL finished) { 336 | [self dismissViewControllerAnimated:NO completion:nil]; 337 | }]; 338 | } 339 | 340 | #pragma mark - getter & setter 341 | 342 | - (NSString *)title { 343 | return [super title]; 344 | } 345 | 346 | - (NSArray *)actions { 347 | return [NSArray arrayWithArray:self.mutableActions]; 348 | } 349 | 350 | - (NSMutableArray *)mutableActions { 351 | if (!_mutableActions) { 352 | _mutableActions = [NSMutableArray array]; 353 | } 354 | return _mutableActions; 355 | } 356 | 357 | - (UILabel *)titleLabel { 358 | if (!_titleLabel) { 359 | _titleLabel = [self creatLabelWithFontSize:20]; 360 | _titleLabel.text = self.title; 361 | _titleLabel.textAlignment = NSTextAlignmentCenter; 362 | [_contentView addSubview:_titleLabel]; 363 | } 364 | return _titleLabel; 365 | } 366 | 367 | - (UILabel *)messageLabel { 368 | if (!_messageLabel) { 369 | _messageLabel = [self creatLabelWithFontSize:15]; 370 | _messageLabel.text = self.message; 371 | _messageLabel.textAlignment = self.messageAlignment; 372 | [_contentView addSubview:_messageLabel]; 373 | } 374 | return _messageLabel; 375 | } 376 | 377 | - (void)setTitle:(NSString *)title { 378 | [super setTitle:title]; 379 | _titleLabel.text = title; 380 | } 381 | 382 | - (void)setMessage:(NSString *)message { 383 | _message = message; 384 | _messageLabel.text = message; 385 | } 386 | 387 | - (void)setMessageAlignment:(NSTextAlignment)messageAlignment { 388 | _messageAlignment = messageAlignment; 389 | _messageLabel.textAlignment = messageAlignment; 390 | } 391 | 392 | @end 393 | -------------------------------------------------------------------------------- /CKCustomAlertViewDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 743752241D7FEF3000BD2415 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 743752231D7FEF3000BD2415 /* main.m */; }; 11 | 743752271D7FEF3000BD2415 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 743752261D7FEF3000BD2415 /* AppDelegate.m */; }; 12 | 7437522A1D7FEF3000BD2415 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 743752291D7FEF3000BD2415 /* ViewController.m */; }; 13 | 7437522D1D7FEF3000BD2415 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7437522B1D7FEF3000BD2415 /* Main.storyboard */; }; 14 | 7437522F1D7FEF3000BD2415 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7437522E1D7FEF3000BD2415 /* Assets.xcassets */; }; 15 | 743752321D7FEF3000BD2415 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 743752301D7FEF3000BD2415 /* LaunchScreen.storyboard */; }; 16 | 7437523B1D7FEF4F00BD2415 /* CKAlertViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7437523A1D7FEF4F00BD2415 /* CKAlertViewController.m */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 7437521F1D7FEF3000BD2415 /* CKCustomAlertViewDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CKCustomAlertViewDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 743752231D7FEF3000BD2415 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 22 | 743752251D7FEF3000BD2415 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | 743752261D7FEF3000BD2415 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | 743752281D7FEF3000BD2415 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 25 | 743752291D7FEF3000BD2415 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 26 | 7437522C1D7FEF3000BD2415 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | 7437522E1D7FEF3000BD2415 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | 743752311D7FEF3000BD2415 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | 743752331D7FEF3000BD2415 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | 743752391D7FEF4F00BD2415 /* CKAlertViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKAlertViewController.h; sourceTree = ""; }; 31 | 7437523A1D7FEF4F00BD2415 /* CKAlertViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CKAlertViewController.m; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | 7437521C1D7FEF3000BD2415 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | 743752161D7FEF3000BD2415 = { 46 | isa = PBXGroup; 47 | children = ( 48 | 743752211D7FEF3000BD2415 /* CKCustomAlertViewDemo */, 49 | 743752201D7FEF3000BD2415 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | 743752201D7FEF3000BD2415 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 7437521F1D7FEF3000BD2415 /* CKCustomAlertViewDemo.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | 743752211D7FEF3000BD2415 /* CKCustomAlertViewDemo */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 7437523C1D7FEF6800BD2415 /* Class */, 65 | 743752251D7FEF3000BD2415 /* AppDelegate.h */, 66 | 743752261D7FEF3000BD2415 /* AppDelegate.m */, 67 | 743752281D7FEF3000BD2415 /* ViewController.h */, 68 | 743752291D7FEF3000BD2415 /* ViewController.m */, 69 | 7437522B1D7FEF3000BD2415 /* Main.storyboard */, 70 | 743752221D7FEF3000BD2415 /* Supporting Files */, 71 | ); 72 | path = CKCustomAlertViewDemo; 73 | sourceTree = ""; 74 | }; 75 | 743752221D7FEF3000BD2415 /* Supporting Files */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 743752231D7FEF3000BD2415 /* main.m */, 79 | 7437522E1D7FEF3000BD2415 /* Assets.xcassets */, 80 | 743752301D7FEF3000BD2415 /* LaunchScreen.storyboard */, 81 | 743752331D7FEF3000BD2415 /* Info.plist */, 82 | ); 83 | name = "Supporting Files"; 84 | sourceTree = ""; 85 | }; 86 | 7437523C1D7FEF6800BD2415 /* Class */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 743752391D7FEF4F00BD2415 /* CKAlertViewController.h */, 90 | 7437523A1D7FEF4F00BD2415 /* CKAlertViewController.m */, 91 | ); 92 | name = Class; 93 | sourceTree = ""; 94 | }; 95 | /* End PBXGroup section */ 96 | 97 | /* Begin PBXNativeTarget section */ 98 | 7437521E1D7FEF3000BD2415 /* CKCustomAlertViewDemo */ = { 99 | isa = PBXNativeTarget; 100 | buildConfigurationList = 743752361D7FEF3000BD2415 /* Build configuration list for PBXNativeTarget "CKCustomAlertViewDemo" */; 101 | buildPhases = ( 102 | 7437521B1D7FEF3000BD2415 /* Sources */, 103 | 7437521C1D7FEF3000BD2415 /* Frameworks */, 104 | 7437521D1D7FEF3000BD2415 /* Resources */, 105 | ); 106 | buildRules = ( 107 | ); 108 | dependencies = ( 109 | ); 110 | name = CKCustomAlertViewDemo; 111 | productName = CKCustomAlertViewDemo; 112 | productReference = 7437521F1D7FEF3000BD2415 /* CKCustomAlertViewDemo.app */; 113 | productType = "com.apple.product-type.application"; 114 | }; 115 | /* End PBXNativeTarget section */ 116 | 117 | /* Begin PBXProject section */ 118 | 743752171D7FEF3000BD2415 /* Project object */ = { 119 | isa = PBXProject; 120 | attributes = { 121 | LastUpgradeCheck = 0730; 122 | ORGANIZATIONNAME = "陈凯"; 123 | TargetAttributes = { 124 | 7437521E1D7FEF3000BD2415 = { 125 | CreatedOnToolsVersion = 7.3.1; 126 | DevelopmentTeam = 6QEB559EPD; 127 | }; 128 | }; 129 | }; 130 | buildConfigurationList = 7437521A1D7FEF3000BD2415 /* Build configuration list for PBXProject "CKCustomAlertViewDemo" */; 131 | compatibilityVersion = "Xcode 3.2"; 132 | developmentRegion = English; 133 | hasScannedForEncodings = 0; 134 | knownRegions = ( 135 | en, 136 | Base, 137 | ); 138 | mainGroup = 743752161D7FEF3000BD2415; 139 | productRefGroup = 743752201D7FEF3000BD2415 /* Products */; 140 | projectDirPath = ""; 141 | projectRoot = ""; 142 | targets = ( 143 | 7437521E1D7FEF3000BD2415 /* CKCustomAlertViewDemo */, 144 | ); 145 | }; 146 | /* End PBXProject section */ 147 | 148 | /* Begin PBXResourcesBuildPhase section */ 149 | 7437521D1D7FEF3000BD2415 /* Resources */ = { 150 | isa = PBXResourcesBuildPhase; 151 | buildActionMask = 2147483647; 152 | files = ( 153 | 743752321D7FEF3000BD2415 /* LaunchScreen.storyboard in Resources */, 154 | 7437522F1D7FEF3000BD2415 /* Assets.xcassets in Resources */, 155 | 7437522D1D7FEF3000BD2415 /* Main.storyboard in Resources */, 156 | ); 157 | runOnlyForDeploymentPostprocessing = 0; 158 | }; 159 | /* End PBXResourcesBuildPhase section */ 160 | 161 | /* Begin PBXSourcesBuildPhase section */ 162 | 7437521B1D7FEF3000BD2415 /* Sources */ = { 163 | isa = PBXSourcesBuildPhase; 164 | buildActionMask = 2147483647; 165 | files = ( 166 | 7437522A1D7FEF3000BD2415 /* ViewController.m in Sources */, 167 | 743752271D7FEF3000BD2415 /* AppDelegate.m in Sources */, 168 | 7437523B1D7FEF4F00BD2415 /* CKAlertViewController.m in Sources */, 169 | 743752241D7FEF3000BD2415 /* main.m in Sources */, 170 | ); 171 | runOnlyForDeploymentPostprocessing = 0; 172 | }; 173 | /* End PBXSourcesBuildPhase section */ 174 | 175 | /* Begin PBXVariantGroup section */ 176 | 7437522B1D7FEF3000BD2415 /* Main.storyboard */ = { 177 | isa = PBXVariantGroup; 178 | children = ( 179 | 7437522C1D7FEF3000BD2415 /* Base */, 180 | ); 181 | name = Main.storyboard; 182 | sourceTree = ""; 183 | }; 184 | 743752301D7FEF3000BD2415 /* LaunchScreen.storyboard */ = { 185 | isa = PBXVariantGroup; 186 | children = ( 187 | 743752311D7FEF3000BD2415 /* Base */, 188 | ); 189 | name = LaunchScreen.storyboard; 190 | sourceTree = ""; 191 | }; 192 | /* End PBXVariantGroup section */ 193 | 194 | /* Begin XCBuildConfiguration section */ 195 | 743752341D7FEF3000BD2415 /* Debug */ = { 196 | isa = XCBuildConfiguration; 197 | buildSettings = { 198 | ALWAYS_SEARCH_USER_PATHS = NO; 199 | CLANG_ANALYZER_NONNULL = YES; 200 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 201 | CLANG_CXX_LIBRARY = "libc++"; 202 | CLANG_ENABLE_MODULES = YES; 203 | CLANG_ENABLE_OBJC_ARC = YES; 204 | CLANG_WARN_BOOL_CONVERSION = YES; 205 | CLANG_WARN_CONSTANT_CONVERSION = YES; 206 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 207 | CLANG_WARN_EMPTY_BODY = YES; 208 | CLANG_WARN_ENUM_CONVERSION = YES; 209 | CLANG_WARN_INT_CONVERSION = YES; 210 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 211 | CLANG_WARN_UNREACHABLE_CODE = YES; 212 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 213 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 214 | COPY_PHASE_STRIP = NO; 215 | DEBUG_INFORMATION_FORMAT = dwarf; 216 | ENABLE_STRICT_OBJC_MSGSEND = YES; 217 | ENABLE_TESTABILITY = YES; 218 | GCC_C_LANGUAGE_STANDARD = gnu99; 219 | GCC_DYNAMIC_NO_PIC = NO; 220 | GCC_NO_COMMON_BLOCKS = YES; 221 | GCC_OPTIMIZATION_LEVEL = 0; 222 | GCC_PREPROCESSOR_DEFINITIONS = ( 223 | "DEBUG=1", 224 | "$(inherited)", 225 | ); 226 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 227 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 228 | GCC_WARN_UNDECLARED_SELECTOR = YES; 229 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 230 | GCC_WARN_UNUSED_FUNCTION = YES; 231 | GCC_WARN_UNUSED_VARIABLE = YES; 232 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 233 | MTL_ENABLE_DEBUG_INFO = YES; 234 | ONLY_ACTIVE_ARCH = YES; 235 | SDKROOT = iphoneos; 236 | TARGETED_DEVICE_FAMILY = "1,2"; 237 | }; 238 | name = Debug; 239 | }; 240 | 743752351D7FEF3000BD2415 /* Release */ = { 241 | isa = XCBuildConfiguration; 242 | buildSettings = { 243 | ALWAYS_SEARCH_USER_PATHS = NO; 244 | CLANG_ANALYZER_NONNULL = YES; 245 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 246 | CLANG_CXX_LIBRARY = "libc++"; 247 | CLANG_ENABLE_MODULES = YES; 248 | CLANG_ENABLE_OBJC_ARC = YES; 249 | CLANG_WARN_BOOL_CONVERSION = YES; 250 | CLANG_WARN_CONSTANT_CONVERSION = YES; 251 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 252 | CLANG_WARN_EMPTY_BODY = YES; 253 | CLANG_WARN_ENUM_CONVERSION = YES; 254 | CLANG_WARN_INT_CONVERSION = YES; 255 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 256 | CLANG_WARN_UNREACHABLE_CODE = YES; 257 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 258 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 259 | COPY_PHASE_STRIP = NO; 260 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 261 | ENABLE_NS_ASSERTIONS = NO; 262 | ENABLE_STRICT_OBJC_MSGSEND = YES; 263 | GCC_C_LANGUAGE_STANDARD = gnu99; 264 | GCC_NO_COMMON_BLOCKS = YES; 265 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 266 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 267 | GCC_WARN_UNDECLARED_SELECTOR = YES; 268 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 269 | GCC_WARN_UNUSED_FUNCTION = YES; 270 | GCC_WARN_UNUSED_VARIABLE = YES; 271 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 272 | MTL_ENABLE_DEBUG_INFO = NO; 273 | SDKROOT = iphoneos; 274 | TARGETED_DEVICE_FAMILY = "1,2"; 275 | VALIDATE_PRODUCT = YES; 276 | }; 277 | name = Release; 278 | }; 279 | 743752371D7FEF3000BD2415 /* Debug */ = { 280 | isa = XCBuildConfiguration; 281 | buildSettings = { 282 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 283 | DEVELOPMENT_TEAM = 6QEB559EPD; 284 | INFOPLIST_FILE = CKCustomAlertViewDemo/Info.plist; 285 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 286 | PRODUCT_BUNDLE_IDENTIFIER = com.MrChen.CKCustomAlertView; 287 | PRODUCT_NAME = "$(TARGET_NAME)"; 288 | }; 289 | name = Debug; 290 | }; 291 | 743752381D7FEF3000BD2415 /* Release */ = { 292 | isa = XCBuildConfiguration; 293 | buildSettings = { 294 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 295 | DEVELOPMENT_TEAM = 6QEB559EPD; 296 | INFOPLIST_FILE = CKCustomAlertViewDemo/Info.plist; 297 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 298 | PRODUCT_BUNDLE_IDENTIFIER = com.MrChen.CKCustomAlertView; 299 | PRODUCT_NAME = "$(TARGET_NAME)"; 300 | }; 301 | name = Release; 302 | }; 303 | /* End XCBuildConfiguration section */ 304 | 305 | /* Begin XCConfigurationList section */ 306 | 7437521A1D7FEF3000BD2415 /* Build configuration list for PBXProject "CKCustomAlertViewDemo" */ = { 307 | isa = XCConfigurationList; 308 | buildConfigurations = ( 309 | 743752341D7FEF3000BD2415 /* Debug */, 310 | 743752351D7FEF3000BD2415 /* Release */, 311 | ); 312 | defaultConfigurationIsVisible = 0; 313 | defaultConfigurationName = Release; 314 | }; 315 | 743752361D7FEF3000BD2415 /* Build configuration list for PBXNativeTarget "CKCustomAlertViewDemo" */ = { 316 | isa = XCConfigurationList; 317 | buildConfigurations = ( 318 | 743752371D7FEF3000BD2415 /* Debug */, 319 | 743752381D7FEF3000BD2415 /* Release */, 320 | ); 321 | defaultConfigurationIsVisible = 0; 322 | defaultConfigurationName = Release; 323 | }; 324 | /* End XCConfigurationList section */ 325 | }; 326 | rootObject = 743752171D7FEF3000BD2415 /* Project object */; 327 | } 328 | --------------------------------------------------------------------------------