CustomActionAlertController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // CustomActionAlertController.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/1/13.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "CustomActionAlertController.h"
  9. /// 左右距离父屏幕边距
  10. #define Left_Spacing 40.0f
  11. /// 内容view的宽度
  12. #define ContentView_Width (SCREEN_WIDTH - Left_Spacing * 2)
  13. typedef void(^SelectedDidBlock)(NSInteger index, NSString *title);
  14. @interface CustomActionAlertController ()
  15. {
  16. }
  17. @property (nonatomic, readwrite, strong) UIView *bgView;
  18. @property (nonatomic, readwrite, strong) UIView *contentView;
  19. @property (nonatomic, readwrite, strong) UILabel *titleLabel;
  20. @property (nonatomic, readwrite, strong) UILabel *messageLabel;
  21. @property (nonatomic, readwrite, strong) UIButton *cancelButton;
  22. @property (nonatomic, readwrite, strong) UIButton *sureButton;
  23. @property (nonatomic, readwrite, copy) NSString *titleStr;
  24. @property (nonatomic, readwrite, copy) NSString *messageStr;
  25. @property (nonatomic, readwrite, copy) NSString *cancelStr;
  26. @property (nonatomic, readwrite, copy) NSString *sureStr;
  27. @property (nonatomic, readwrite, copy) SelectedDidBlock selectedDidBlock;
  28. //取消外部触摸消失
  29. @property (nonatomic, assign) BOOL cancelFullScreenTapGes;
  30. //多行靠左显示,字体放大
  31. @property (nonatomic, assign) BOOL muitipleLine;
  32. @end
  33. @implementation CustomActionAlertController
  34. #pragma mark — Publiv
  35. - (instancetype)initWithTitle:(nullable NSString *)title message:(nullable NSString *)message sure:(nullable NSString *)sure cancel:(nullable NSString *)cancel selctedBlock:(void(^)(NSInteger index, NSString *title))block {
  36. if (self = [super init]) {
  37. self.titleStr = [title copy];
  38. self.messageStr = [message copy];
  39. self.sureStr = [sure copy];
  40. self.cancelStr = [cancel copy];
  41. self.selectedDidBlock = block;
  42. self.view.frame = [UIScreen mainScreen].bounds;
  43. self.view.backgroundColor = Color_Clear;
  44. }
  45. return self;
  46. }
  47. - (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{
  48. if (self = [super init]) {
  49. self.cancelFullScreenTapGes = state;
  50. self.titleStr = [title copy];
  51. self.messageStr = [message copy];
  52. self.sureStr = [sure copy];
  53. self.cancelStr = [cancel copy];
  54. self.selectedDidBlock = block;
  55. self.view.frame = [UIScreen mainScreen].bounds;
  56. self.view.backgroundColor = Color_Clear;
  57. }
  58. return self;
  59. }
  60. - (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{
  61. if (self = [super init]) {
  62. self.muitipleLine = muitipleLine;
  63. self.titleStr = [title copy];
  64. self.messageStr = [message copy];
  65. self.sureStr = [sure copy];
  66. self.cancelStr = [cancel copy];
  67. self.selectedDidBlock = block;
  68. self.view.frame = [UIScreen mainScreen].bounds;
  69. self.view.backgroundColor = Color_Clear;
  70. }
  71. return self;
  72. }
  73. - (void)show {
  74. UIWindow *window = [UIWindow lastWindow];
  75. UIViewController *appRootVC = window.rootViewController;
  76. UIViewController *topVC = appRootVC;
  77. if (topVC.presentedViewController) {
  78. topVC = topVC.presentedViewController;
  79. if ([topVC isKindOfClass:[UINavigationController class]]) {
  80. UINavigationController *nav = (UINavigationController *)topVC;
  81. [nav.topViewController addChildViewController:self];
  82. [nav.topViewController.view addSubview:self.view];
  83. }else {
  84. [topVC addChildViewController:self];
  85. [topVC .view addSubview:self.view];
  86. }
  87. }else {
  88. if ([appRootVC isKindOfClass:[UINavigationController class]]) {
  89. UINavigationController *nav = (UINavigationController *)appRootVC;
  90. [nav.topViewController addChildViewController:self];
  91. [nav.topViewController.view addSubview:self.view];
  92. }else {
  93. [appRootVC addChildViewController:self];
  94. [appRootVC .view addSubview:self.view];
  95. }
  96. }
  97. [self showAnimation];
  98. [self showBackground];
  99. }
  100. - (void)showWithController:(UIViewController *)controller {
  101. [controller addChildViewController:self];
  102. [controller.view addSubview:self.view];
  103. [self showAnimation];
  104. [self showBackground];
  105. }
  106. - (void)dismiss {
  107. dispatch_async(dispatch_get_main_queue(), ^{
  108. [self hiddenAnimation];
  109. [self.view removeFromSuperview];
  110. [self removeFromParentViewController];
  111. });
  112. }
  113. - (void)setupSureButtonBackgroundImage:(UIImage *)image {
  114. if (_sureButton) {
  115. [_sureButton setBackgroundImage:image forState:UIControlStateNormal];
  116. }
  117. }
  118. /// 重写init方法,配置你想要的属性
  119. - (instancetype)init
  120. {
  121. self = [super init];
  122. if (self) {
  123. }
  124. return self;
  125. }
  126. - (void)viewDidLoad {
  127. [super viewDidLoad];
  128. /// 设置
  129. [self _setup];
  130. /// 设置导航栏
  131. [self _setupNavigationItem];
  132. /// 设置子控件
  133. [self _setupSubViews];
  134. /// 布局子空间
  135. [self _makeSubViewsConstraints];
  136. }
  137. #pragma mark — Override
  138. - (void)bindViewModel {
  139. }
  140. #pragma mark - 事件处理Or辅助方法
  141. - (void)cancelButtonAction:(id)sender {
  142. if (self.selectedDidBlock) {
  143. self.selectedDidBlock(0, self.cancelStr);
  144. }
  145. [self dismiss];
  146. }
  147. - (void)sureButtonAction:(id)sender {
  148. if (self.selectedDidBlock) {
  149. self.selectedDidBlock(1, self.sureStr);
  150. }
  151. [self dismiss];
  152. }
  153. #pragma mark - Animation  提示框弹出时的动画效果
  154. - (void)showAnimation {
  155. CAKeyframeAnimation *alertViewshowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  156. alertViewshowAnimation .duration = 0.4;
  157. alertViewshowAnimation .values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
  158. [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
  159. [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
  160. [NSValue valueWithCATransform3D:CATransform3DIdentity]];
  161. alertViewshowAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f, @1.0f];
  162. alertViewshowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
  163. [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
  164. [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  165. [self.contentView.layer addAnimation:alertViewshowAnimation forKey:nil];
  166. }
  167. - (void)showBackground
  168. {
  169. self.bgView.alpha = 0;
  170. [UIView beginAnimations:@"fadeIn" context:nil];
  171. [UIView setAnimationDuration:0.35];
  172. self.bgView.alpha = 0.35;
  173. [UIView commitAnimations];
  174. }
  175. - (void)hiddenAnimation {
  176. [UIView beginAnimations:@"fadeIn" context:nil];
  177. [UIView setAnimationDuration:0.35];
  178. self.bgView.alpha = 0.0;
  179. [UIView commitAnimations];
  180. }
  181. #pragma mark - 初始化
  182. - (void)_setup{
  183. }
  184. #pragma mark - 设置导航栏
  185. - (void)_setupNavigationItem{
  186. }
  187. #pragma mark - 设置子控件
  188. - (void)_setupSubViews{
  189. [self.view addSubview:self.bgView];
  190. [self.view addSubview:self.contentView];
  191. CGFloat allHeight = 18.0f;
  192. if (StringIsNotEmpty(self.titleStr)) {
  193. [self.contentView addSubview:self.titleLabel];
  194. self.titleLabel.text = self.titleStr;
  195. self.titleLabel.frame = CGRectMake(20.0f, allHeight, ContentView_Width - 40.0f, 25.0f);
  196. allHeight += 25.0f;
  197. allHeight += 18.0;
  198. }
  199. if (StringIsNotEmpty(self.messageStr)) {
  200. [self.contentView addSubview:self.messageLabel];
  201. CGFloat msgHeight = [HandleString autoLabelWith:self.messageStr withSize:CGSizeMake(ContentView_Width - 40.0f, MAXFLOAT) withFont:self.messageLabel.font withLines:0].height;
  202. self.messageLabel.text = self.messageStr;
  203. self.messageLabel.frame = CGRectMake(20.0f, allHeight, ContentView_Width - 40.0f, msgHeight);
  204. allHeight += msgHeight;
  205. allHeight += 25.0f;
  206. }
  207. if (StringIsNotEmpty(self.cancelStr) || StringIsNotEmpty(self.sureStr)) {
  208. CGFloat buttonWidth = (ContentView_Width - 10.0f - 40.0f)/2;
  209. if (StringIsNotEmpty(self.cancelStr) && StringIsEmpty(self.sureStr)) {
  210. // 有取消按钮 没有确认按钮
  211. [self.contentView addSubview:self.cancelButton];
  212. [self.cancelButton setTitle:self.cancelStr forState:UIControlStateNormal];
  213. self.cancelButton.frame = CGRectMake((ContentView_Width - buttonWidth)/2, allHeight, buttonWidth, 40.0f);
  214. [self.cancelButton addViewBorder:Color_Clear redian:20];
  215. }else if (StringIsNotEmpty(self.sureStr) && StringIsEmpty(self.cancelStr)) {
  216. // 有确认按钮 没有取消按钮
  217. [self.contentView addSubview:self.sureButton];
  218. [self.sureButton setTitle:self.sureStr forState:UIControlStateNormal];
  219. self.sureButton.frame = CGRectMake((ContentView_Width - buttonWidth)/2, allHeight, buttonWidth, 40.0f);
  220. [self.sureButton addViewBorder:Color_Clear redian:20];
  221. }else {
  222. // 两个按钮都有
  223. [self.contentView addSubview:self.cancelButton];
  224. [self.contentView addSubview:self.sureButton];
  225. [self.cancelButton setTitle:self.cancelStr forState:UIControlStateNormal];
  226. [self.sureButton setTitle:self.sureStr forState:UIControlStateNormal];
  227. self.cancelButton.frame = CGRectMake(20.0f, allHeight, buttonWidth, 40.0f);
  228. self.sureButton.frame = CGRectMake(self.cancelButton.f_right + 10.0f, allHeight, buttonWidth, 40.0f);
  229. [self.cancelButton addViewBorder:Color_Clear redian:20];
  230. [self.sureButton addViewBorder:Color_Clear redian:20];
  231. }
  232. // 确认按钮和取消按钮有一个就可以增加同样的高度
  233. allHeight += 40.0f;
  234. allHeight += 20.0f;
  235. }
  236. // 居中
  237. self.contentView.frame = CGRectMake(Left_Spacing, (SCREEN_HEIGHT - allHeight)/2, ContentView_Width, allHeight);
  238. self.contentView.center = self.view.center;
  239. [self.contentView addViewBorder:Color_Clear redian:8];
  240. if (self.cancelFullScreenTapGes == NO) {
  241. // 给背景添加取消操作
  242. self.bgView.userInteractionEnabled = YES;
  243. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelButtonAction:)];
  244. [self.bgView addGestureRecognizer:tap];
  245. }
  246. }
  247. #pragma mark - 布局子控件
  248. - (void)_makeSubViewsConstraints{
  249. }
  250. #pragma mark - Setter & Getter
  251. - (UIView *)bgView {
  252. if (!_bgView) {
  253. _bgView = [[UIView alloc] initWithFrame:SCREEN_BOUNDS];
  254. _bgView.backgroundColor = Color_Black;
  255. }
  256. return _bgView;
  257. }
  258. - (UIView *)contentView {
  259. if (!_contentView) {
  260. _contentView = [UIView new];
  261. _contentView.backgroundColor = Color_White;
  262. }
  263. return _contentView;;
  264. }
  265. - (UILabel *)titleLabel {
  266. if (!_titleLabel) {
  267. _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_S(18)];
  268. _titleLabel.textAlignment = NSTextAlignmentCenter;
  269. _titleLabel.adjustsFontSizeToFitWidth = YES;
  270. _titleLabel.minimumScaleFactor = 0.5;
  271. }
  272. return _titleLabel;
  273. }
  274. - (UILabel *)messageLabel {
  275. if (!_messageLabel) {
  276. if (self.muitipleLine) {
  277. _messageLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font(15)];
  278. _messageLabel.numberOfLines = 0;
  279. _messageLabel.textAlignment = NSTextAlignmentLeft;
  280. }else{
  281. _messageLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font(14)];
  282. _messageLabel.numberOfLines = 10;
  283. _messageLabel.textAlignment = NSTextAlignmentCenter;
  284. _messageLabel.adjustsFontSizeToFitWidth = YES;
  285. _messageLabel.minimumScaleFactor = 0.5;
  286. }
  287. }
  288. return _messageLabel;
  289. }
  290. - (UIButton *)cancelButton {
  291. if (!_cancelButton) {
  292. _cancelButton = [UIButton createButtonTextColor:Color_TextFont textFont:Font(16)];
  293. [_cancelButton setBackgroundColor:Color_Background];
  294. [_cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  295. }
  296. return _cancelButton;
  297. }
  298. - (UIButton *)sureButton {
  299. if (!_sureButton) {
  300. _sureButton = [UIButton createButtonTextColor:Color_White textFont:Font(16)];
  301. if (self.muitipleLine) {
  302. [_sureButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[ColorFromHexString(@"#5D26FF"), ColorFromHexString(@"#8359FF")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake((ContentView_Width - 10.0f - 40.0f)/2, 40.0f)] forState:UIControlStateNormal];
  303. }else{
  304. [_sureButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[ColorFromHexString(@"#5D26FF"), ColorFromHexString(@"#9059FF")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake((ContentView_Width - 10.0f - 40.0f)/2, 40.0f)] forState:UIControlStateNormal];
  305. }
  306. [_sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  307. }
  308. return _sureButton;
  309. }
  310. @end