123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- //
- // CustomActionAlertController.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/1/13.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "CustomActionAlertController.h"
- /// 左右距离父屏幕边距
- #define Left_Spacing 40.0f
- /// 内容view的宽度
- #define ContentView_Width (SCREEN_WIDTH - Left_Spacing * 2)
- typedef void(^SelectedDidBlock)(NSInteger index, NSString *title);
- @interface CustomActionAlertController ()
- {
-
- }
- @property (nonatomic, readwrite, strong) UIView *bgView;
- @property (nonatomic, readwrite, strong) UIView *contentView;
- @property (nonatomic, readwrite, strong) UILabel *titleLabel;
- @property (nonatomic, readwrite, strong) UILabel *messageLabel;
- @property (nonatomic, readwrite, strong) UIButton *cancelButton;
- @property (nonatomic, readwrite, strong) UIButton *sureButton;
- @property (nonatomic, readwrite, copy) NSString *titleStr;
- @property (nonatomic, readwrite, copy) NSString *messageStr;
- @property (nonatomic, readwrite, copy) NSString *cancelStr;
- @property (nonatomic, readwrite, copy) NSString *sureStr;
- @property (nonatomic, readwrite, copy) SelectedDidBlock selectedDidBlock;
- //取消外部触摸消失
- @property (nonatomic, assign) BOOL cancelFullScreenTapGes;
- //多行靠左显示,字体放大
- @property (nonatomic, assign) BOOL muitipleLine;
- @end
- @implementation CustomActionAlertController
- #pragma mark — Publiv
- - (instancetype)initWithTitle:(nullable NSString *)title message:(nullable NSString *)message sure:(nullable NSString *)sure cancel:(nullable NSString *)cancel selctedBlock:(void(^)(NSInteger index, NSString *title))block {
- if (self = [super init]) {
-
- self.titleStr = [title copy];
- self.messageStr = [message copy];
- self.sureStr = [sure copy];
- self.cancelStr = [cancel copy];
- self.selectedDidBlock = block;
-
- self.view.frame = [UIScreen mainScreen].bounds;
- self.view.backgroundColor = Color_Clear;
- }
- return self;
- }
- - (instancetype)initWithTitle:(nullable NSString *)title message:(nullable NSString *)message sure:(nullable NSString *)sure cancel:(nullable NSString *)cancel selctedBlock:(void(^)(NSInteger index, NSString *title))block andCancelFullScreenTapGes:(BOOL )state{
- if (self = [super init]) {
- self.cancelFullScreenTapGes = state;
- self.titleStr = [title copy];
- self.messageStr = [message copy];
- self.sureStr = [sure copy];
- self.cancelStr = [cancel copy];
- self.selectedDidBlock = block;
-
- self.view.frame = [UIScreen mainScreen].bounds;
- self.view.backgroundColor = Color_Clear;
- }
- return self;
- }
- - (instancetype)initWithTitle:(nullable NSString *)title message:(nullable NSString *)message sure:(nullable NSString *)sure cancel:(nullable NSString *)cancel selctedBlock:(void(^)(NSInteger index, NSString *title))block andMuitipleLine:(BOOL )muitipleLine{
- if (self = [super init]) {
- self.muitipleLine = muitipleLine;
- self.titleStr = [title copy];
- self.messageStr = [message copy];
- self.sureStr = [sure copy];
- self.cancelStr = [cancel copy];
- self.selectedDidBlock = block;
-
- self.view.frame = [UIScreen mainScreen].bounds;
- self.view.backgroundColor = Color_Clear;
- }
- return self;
- }
- - (void)show {
-
- UIWindow *window = [UIWindow lastWindow];
- UIViewController *appRootVC = window.rootViewController;
- UIViewController *topVC = appRootVC;
- if (topVC.presentedViewController) {
- topVC = topVC.presentedViewController;
- if ([topVC isKindOfClass:[UINavigationController class]]) {
- UINavigationController *nav = (UINavigationController *)topVC;
- [nav.topViewController addChildViewController:self];
- [nav.topViewController.view addSubview:self.view];
- }else {
- [topVC addChildViewController:self];
- [topVC .view addSubview:self.view];
- }
- }else {
- if ([appRootVC isKindOfClass:[UINavigationController class]]) {
- UINavigationController *nav = (UINavigationController *)appRootVC;
- [nav.topViewController addChildViewController:self];
- [nav.topViewController.view addSubview:self.view];
- }else {
- [appRootVC addChildViewController:self];
- [appRootVC .view addSubview:self.view];
- }
- }
-
- [self showAnimation];
- [self showBackground];
- }
- - (void)showWithController:(UIViewController *)controller {
-
- [controller addChildViewController:self];
- [controller.view addSubview:self.view];
-
- [self showAnimation];
- [self showBackground];
- }
- - (void)dismiss {
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self hiddenAnimation];
- [self.view removeFromSuperview];
- [self removeFromParentViewController];
- });
- }
- - (void)setupSureButtonBackgroundImage:(UIImage *)image {
- if (_sureButton) {
- [_sureButton setBackgroundImage:image forState:UIControlStateNormal];
- }
- }
- /// 重写init方法,配置你想要的属性
- - (instancetype)init
- {
- self = [super init];
- if (self) {
-
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- /// 设置
- [self _setup];
-
- /// 设置导航栏
- [self _setupNavigationItem];
-
- /// 设置子控件
- [self _setupSubViews];
-
- /// 布局子空间
- [self _makeSubViewsConstraints];
- }
- #pragma mark — Override
- - (void)bindViewModel {
-
- }
- #pragma mark - 事件处理Or辅助方法
- - (void)cancelButtonAction:(id)sender {
- if (self.selectedDidBlock) {
- self.selectedDidBlock(0, self.cancelStr);
- }
- [self dismiss];
- }
- - (void)sureButtonAction:(id)sender {
- if (self.selectedDidBlock) {
- self.selectedDidBlock(1, self.sureStr);
- }
- [self dismiss];
- }
- #pragma mark - Animation 提示框弹出时的动画效果
- - (void)showAnimation {
-
- CAKeyframeAnimation *alertViewshowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
- alertViewshowAnimation .duration = 0.4;
- alertViewshowAnimation .values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
- [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
- [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
- [NSValue valueWithCATransform3D:CATransform3DIdentity]];
- alertViewshowAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f, @1.0f];
- alertViewshowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
- [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
- [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
- [self.contentView.layer addAnimation:alertViewshowAnimation forKey:nil];
- }
- - (void)showBackground
- {
- self.bgView.alpha = 0;
- [UIView beginAnimations:@"fadeIn" context:nil];
- [UIView setAnimationDuration:0.35];
- self.bgView.alpha = 0.35;
- [UIView commitAnimations];
- }
- - (void)hiddenAnimation {
- [UIView beginAnimations:@"fadeIn" context:nil];
- [UIView setAnimationDuration:0.35];
- self.bgView.alpha = 0.0;
- [UIView commitAnimations];
- }
- #pragma mark - 初始化
- - (void)_setup{
-
- }
- #pragma mark - 设置导航栏
- - (void)_setupNavigationItem{
-
- }
- #pragma mark - 设置子控件
- - (void)_setupSubViews{
- [self.view addSubview:self.bgView];
- [self.view addSubview:self.contentView];
-
- CGFloat allHeight = 18.0f;
- if (StringIsNotEmpty(self.titleStr)) {
- [self.contentView addSubview:self.titleLabel];
- self.titleLabel.text = self.titleStr;
- self.titleLabel.frame = CGRectMake(20.0f, allHeight, ContentView_Width - 40.0f, 25.0f);
- allHeight += 25.0f;
- allHeight += 18.0;
- }
- if (StringIsNotEmpty(self.messageStr)) {
- [self.contentView addSubview:self.messageLabel];
- CGFloat msgHeight = [HandleString autoLabelWith:self.messageStr withSize:CGSizeMake(ContentView_Width - 40.0f, MAXFLOAT) withFont:self.messageLabel.font withLines:0].height;
- self.messageLabel.text = self.messageStr;
- self.messageLabel.frame = CGRectMake(20.0f, allHeight, ContentView_Width - 40.0f, msgHeight);
- allHeight += msgHeight;
- allHeight += 25.0f;
- }
- if (StringIsNotEmpty(self.cancelStr) || StringIsNotEmpty(self.sureStr)) {
-
- CGFloat buttonWidth = (ContentView_Width - 10.0f - 40.0f)/2;
- if (StringIsNotEmpty(self.cancelStr) && StringIsEmpty(self.sureStr)) {
- // 有取消按钮 没有确认按钮
- [self.contentView addSubview:self.cancelButton];
- [self.cancelButton setTitle:self.cancelStr forState:UIControlStateNormal];
- self.cancelButton.frame = CGRectMake((ContentView_Width - buttonWidth)/2, allHeight, buttonWidth, 40.0f);
- [self.cancelButton addViewBorder:Color_Clear redian:20];
- }else if (StringIsNotEmpty(self.sureStr) && StringIsEmpty(self.cancelStr)) {
- // 有确认按钮 没有取消按钮
- [self.contentView addSubview:self.sureButton];
- [self.sureButton setTitle:self.sureStr forState:UIControlStateNormal];
- self.sureButton.frame = CGRectMake((ContentView_Width - buttonWidth)/2, allHeight, buttonWidth, 40.0f);
- [self.sureButton addViewBorder:Color_Clear redian:20];
- }else {
- // 两个按钮都有
- [self.contentView addSubview:self.cancelButton];
- [self.contentView addSubview:self.sureButton];
- [self.cancelButton setTitle:self.cancelStr forState:UIControlStateNormal];
- [self.sureButton setTitle:self.sureStr forState:UIControlStateNormal];
- self.cancelButton.frame = CGRectMake(20.0f, allHeight, buttonWidth, 40.0f);
- self.sureButton.frame = CGRectMake(self.cancelButton.f_right + 10.0f, allHeight, buttonWidth, 40.0f);
- [self.cancelButton addViewBorder:Color_Clear redian:20];
- [self.sureButton addViewBorder:Color_Clear redian:20];
- }
-
- // 确认按钮和取消按钮有一个就可以增加同样的高度
- allHeight += 40.0f;
- allHeight += 20.0f;
- }
-
- // 居中
- self.contentView.frame = CGRectMake(Left_Spacing, (SCREEN_HEIGHT - allHeight)/2, ContentView_Width, allHeight);
- self.contentView.center = self.view.center;
-
- [self.contentView addViewBorder:Color_Clear redian:8];
-
- if (self.cancelFullScreenTapGes == NO) {
- // 给背景添加取消操作
- self.bgView.userInteractionEnabled = YES;
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelButtonAction:)];
- [self.bgView addGestureRecognizer:tap];
- }
-
- }
- #pragma mark - 布局子控件
- - (void)_makeSubViewsConstraints{
-
- }
- #pragma mark - Setter & Getter
- - (UIView *)bgView {
- if (!_bgView) {
- _bgView = [[UIView alloc] initWithFrame:SCREEN_BOUNDS];
- _bgView.backgroundColor = Color_Black;
- }
- return _bgView;
- }
- - (UIView *)contentView {
- if (!_contentView) {
- _contentView = [UIView new];
- _contentView.backgroundColor = Color_White;
- }
- return _contentView;;
- }
- - (UILabel *)titleLabel {
- if (!_titleLabel) {
- _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_S(18)];
- _titleLabel.textAlignment = NSTextAlignmentCenter;
- _titleLabel.adjustsFontSizeToFitWidth = YES;
- _titleLabel.minimumScaleFactor = 0.5;
- }
- return _titleLabel;
- }
- - (UILabel *)messageLabel {
- if (!_messageLabel) {
- if (self.muitipleLine) {
- _messageLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font(15)];
- _messageLabel.numberOfLines = 0;
- _messageLabel.textAlignment = NSTextAlignmentLeft;
- }else{
- _messageLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font(14)];
- _messageLabel.numberOfLines = 10;
- _messageLabel.textAlignment = NSTextAlignmentCenter;
- _messageLabel.adjustsFontSizeToFitWidth = YES;
- _messageLabel.minimumScaleFactor = 0.5;
- }
- }
- return _messageLabel;
- }
- - (UIButton *)cancelButton {
- if (!_cancelButton) {
- _cancelButton = [UIButton createButtonTextColor:Color_TextFont textFont:Font(16)];
- [_cancelButton setBackgroundColor:Color_Background];
- [_cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _cancelButton;
- }
- - (UIButton *)sureButton {
- if (!_sureButton) {
- _sureButton = [UIButton createButtonTextColor:Color_White textFont:Font(16)];
- if (self.muitipleLine) {
- [_sureButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[ColorFromHexString(@"#5D26FF"), ColorFromHexString(@"#8359FF")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake((ContentView_Width - 10.0f - 40.0f)/2, 40.0f)] forState:UIControlStateNormal];
- }else{
- [_sureButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[ColorFromHexString(@"#5D26FF"), ColorFromHexString(@"#9059FF")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake((ContentView_Width - 10.0f - 40.0f)/2, 40.0f)] forState:UIControlStateNormal];
- }
- [_sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _sureButton;
- }
- @end
|