SPSBActionSheetViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // SPSBActionSheetViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/13.
  6. //
  7. #import "SPSBActionSheetViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. @interface SPSBActionSheetViewItem : NSObject
  10. @property (nonatomic, strong) NSString *spsb_title;
  11. @property (nonatomic, strong, nullable) UIColor *spsb_titleColor;
  12. @property (nonatomic, strong, nullable) UIFont *spsb_font;
  13. @property (nonatomic, strong) void(^spsb_action)(void);
  14. @end
  15. @implementation SPSBActionSheetViewItem
  16. @end
  17. @interface SPSBActionSheetViewController () {
  18. NSMutableArray<SPSBActionSheetViewItem *> *_items;
  19. UIView *_Nullable _topView;
  20. CGFloat _topViewHeight;
  21. UIView *_contentView;
  22. UIView *_cancelView;
  23. }
  24. @end
  25. @implementation SPSBActionSheetViewController
  26. - (instancetype)init {
  27. self = [super init];
  28. if (!self) return nil;
  29. _items = NSMutableArray.new;
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self setupUI];
  35. }
  36. #define spsb_itemTag 4000
  37. #pragma mark - Action
  38. - (void)chosAction:(UIButton *)sender {
  39. if (sender.tag == 3000) {
  40. [self closeAction];
  41. return;
  42. }
  43. NSInteger index = sender.tag - spsb_itemTag;
  44. if (index >= _items.count) return;
  45. [self closeActionCompletion:^{
  46. self->_items[index].spsb_action();
  47. }];
  48. }
  49. - (void)closeAction {
  50. [self closeActionCompletion:nil];
  51. }
  52. - (void)closeActionCompletion: (void (^_Nullable)(void))completion {
  53. [_contentView makeConstraints:^(JXHConstraintMaker *make) {
  54. make.top.update(0);
  55. }];
  56. [UIView animateWithDuration:0.3 animations:^{
  57. [self.view layoutSubviews];
  58. self.view.backgroundColor = spsb_000000_color(0);
  59. } completion:^(BOOL finished) {
  60. [self dismissViewControllerAnimated:false completion:completion];
  61. }];
  62. }
  63. - (void)addTopView:(UIView *)view height:(CGFloat)height {
  64. _topView = view;
  65. _topViewHeight = height;
  66. }
  67. - (void)addActionWithTitle:(NSString *)title action:(void(^)(void))action {
  68. [self addActionWithTitle:title titleColor:nil font:nil action:action];
  69. }
  70. - (void)addActionWithTitle:(NSString *)title titleColor:(UIColor *) titleColor action:(void(^)(void))action {
  71. [self addActionWithTitle:title titleColor:titleColor font:nil action:action];
  72. }
  73. - (void)addActionWithTitle:(NSString *)title titleColor:(nullable UIColor *) titleColor font:(nullable UIFont *)font action:(void(^)(void))action {
  74. SPSBActionSheetViewItem *item = SPSBActionSheetViewItem.new;
  75. item.spsb_title = title;
  76. item.spsb_titleColor = titleColor;
  77. item.spsb_font = font;
  78. item.spsb_action = action;
  79. [_items addObject:item];
  80. }
  81. - (void)viewDidLayoutSubviews {
  82. if (jxh_safeInsets(self.view).bottom == 0) {
  83. [_cancelView makeConstraints:^(JXHConstraintMaker *make) {
  84. make.bottom.equalTo(self->_contentView.safebottom).update(-16);
  85. }];
  86. }
  87. }
  88. #pragma mark - Overwrite
  89. - (void)showAnimation {
  90. [_contentView makeConstraints:^(JXHConstraintMaker *make) {
  91. make.top.update(-jxh_viewHeight(self->_contentView));
  92. }];
  93. [UIView animateWithDuration:0.3 animations:^{
  94. [self.view layoutSubviews];
  95. self.view.backgroundColor = spsb_000000_color(SPSBPopupViewBackgroundAlpha);
  96. }];
  97. }
  98. - (void)setupAnimation {
  99. self.view.backgroundColor = spsb_000000_color(0.f);
  100. }
  101. #define spsb_buttonHeight 50
  102. #pragma mark - UI
  103. - (void)setupUI {
  104. [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction)]];
  105. _contentView = UIView.new;
  106. [self.view addSubview:_contentView];
  107. UIView *itemView = UIView.new;
  108. itemView.backgroundColor = spsb_FFFFFF_color(1.f);
  109. [_contentView addSubview:itemView];
  110. [itemView makeConstraints:^(JXHConstraintMaker *make) {
  111. make.top.and.leading.and.trailing.equalTo(0);
  112. }];
  113. UIView *tempView = nil;
  114. if (_topView) {
  115. _contentView.backgroundColor = _topView.backgroundColor;
  116. [itemView addSubview:_topView];
  117. [_topView makeConstraints:^(JXHConstraintMaker *make) {
  118. make.leading.and.trailing.and.top.equalTo(0);
  119. if (self->_topViewHeight > 0) {
  120. make.height.equalTo(self->_topViewHeight);
  121. }
  122. }];
  123. tempView = _topView;
  124. } else {
  125. _contentView.backgroundColor = spsb_F5F5F5_color();
  126. }
  127. NSInteger i = 0;
  128. for (SPSBActionSheetViewItem *item in _items) {
  129. UIButton *button = [self createButtonWithTitle:item.spsb_title titleColor:item.spsb_titleColor ?: spsb_000000_color(0.8) font:item.spsb_font ?: spsb_font(17) tag:spsb_itemTag + i];
  130. [itemView addSubview:button];
  131. [button makeConstraints:^(JXHConstraintMaker *make) {
  132. if (tempView) {
  133. make.top.equalTo(tempView.bottom);
  134. } else {
  135. make.top.equalTo(0);
  136. }
  137. make.leading.and.trailing.equalTo(0);
  138. make.height.equalTo(spsb_buttonHeight);
  139. }];
  140. if (i != _items.count - 1) {
  141. [button createLineWithLocation:JXHLineLocationBottom];
  142. }
  143. i ++;
  144. tempView = button;
  145. }
  146. if (tempView) {
  147. [tempView makeConstraints:^(JXHConstraintMaker *make) {
  148. make.bottom.equalTo(0);
  149. }];
  150. } else {
  151. [self dismissViewControllerAnimated:false completion:nil];
  152. }
  153. _cancelView = UIView.new;
  154. _cancelView.backgroundColor = spsb_FFFFFF_color(1.f);
  155. [_contentView addSubview:_cancelView];
  156. [_cancelView makeConstraints:^(JXHConstraintMaker *make) {
  157. make.leading.and.trailing.equalTo(itemView);
  158. make.bottom.equalTo(self->_contentView.safebottom);
  159. make.top.equalTo(itemView.bottom).offset(10);
  160. }];
  161. UIButton *cancelButton = [self createButtonWithTitle:@"取消" titleColor:spsb_000000_color(0.8) font:spsb_font(17) tag:3000];
  162. [_cancelView addSubview:cancelButton];
  163. [cancelButton makeConstraints:^(JXHConstraintMaker *make) {
  164. make.edges.equalTo(0);
  165. make.height.equalTo(spsb_buttonHeight);
  166. }];
  167. [_contentView makeConstraints:^(JXHConstraintMaker *make) {
  168. make.leading.and.trailing.equalTo(0);
  169. make.top.equalTo(self.view.bottom);
  170. }];
  171. }
  172. - (UIButton *)createButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font tag:(NSInteger)tag {
  173. UIButton *button = [UIButton convenienceWithFont:font target:self action:@selector(chosAction:)];
  174. button.tag = tag;
  175. [button setTitle:title titleColor:titleColor image:nil backgroundImage:jxh_createImage(spsb_clear_color(), (CGSize){spsb_buttonHeight, spsb_buttonHeight}) state:JXHButtonControlStateNormal];
  176. [button setBackgroundImage:jxh_createImage(spsb_000000_color(0.08), (CGSize){spsb_buttonHeight, spsb_buttonHeight}) state:JXHButtonControlStateHighlighted];
  177. return button;
  178. }
  179. @end
  180. #undef spsb_itemTag
  181. #undef spsb_buttonHeight