CustomActionSheet.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // CustomActionSheet.m
  3. // MIT_Shop
  4. //
  5. // Created by 翟玉磊 on 2017/10/17.
  6. // Copyright © 2017年 翟玉磊. All rights reserved.
  7. //
  8. #import "CustomActionSheet.h"
  9. #define CancelButtonHeight BUTTON_HEIGHT //取消按钮的高度
  10. #define TitleViewHeight 50.0f //标题高度
  11. #define OtherButtonHeight 50.0f //其他按钮的高度(就是cell的高度)
  12. #define OtherButtonNumMax 4 //其他按钮最大数量4个 超过四个则开启滚动
  13. #define DefaultMarkImageName [UIImage imageNamed:@"goods_gou"] //默认标示图片
  14. @interface CustomActionSheet ()<UITableViewDelegate, UITableViewDataSource>
  15. {
  16. CGFloat _infoViewHeight;
  17. BOOL _isCanScroll; //是否可以滚动当otherbutto超过默认最大数量时,则可以滑动
  18. CGRect _showInfoViewRect; //显示infoView的rect
  19. CGRect _hiddenInfoViewRect; //隐藏infoView的rect
  20. NSInteger _selectedCellIndex; //选中的索引 默认第一个:0
  21. }
  22. @property (nonatomic, strong) UIButton *bgButton;
  23. @property (nonatomic, strong) UIView *infoView;
  24. @property (nonatomic, strong) UILabel *titleLabel;
  25. @property (nonatomic, strong) UIButton *cancelButton;
  26. @property (nonatomic, strong) UITableView *tableView;
  27. @property (nonatomic, strong) UIImageView *markImageView; //选中的标示
  28. @end
  29. static NSString * const cellId = @"UITableViewCellActionSheetId";
  30. @implementation CustomActionSheet
  31. + (instancetype)showActionSheetWithTitle:(NSString *)title cancelTitle:(NSString *)cancel otherButtonTitles:(NSArray *)titles {
  32. CustomActionSheet *sheet = [[CustomActionSheet alloc] initWithFrame:[UIScreen mainScreen].bounds];
  33. sheet.title = title;
  34. sheet.cancelTitle = cancel;
  35. sheet.otherButtonTitleArray = [NSMutableArray arrayWithArray:titles];
  36. [sheet initValue];
  37. [sheet buildView];
  38. [sheet addAction];
  39. return sheet;
  40. }
  41. - (void)initValue {
  42. //默认选中条数
  43. _selectedCellIndex = 0;
  44. //计算高度infoView的高度
  45. _infoViewHeight = 0.0f;
  46. if (_title.length > 0 && _title) {
  47. _infoViewHeight += TitleViewHeight;
  48. }
  49. if (_otherButtonTitleArray.count > 0 && _otherButtonTitleArray) {
  50. _infoViewHeight += ((_otherButtonTitleArray.count <= OtherButtonNumMax ? _otherButtonTitleArray.count : OtherButtonNumMax) * OtherButtonHeight);
  51. _infoViewHeight += (_otherButtonTitleArray.count <= OtherButtonNumMax ? 0 : 10);
  52. }
  53. if (_cancelTitle.length > 0 && _cancelTitle) {
  54. _infoViewHeight += CancelButtonHeight;
  55. }
  56. //计算infoView的rect
  57. _showInfoViewRect = CGRectMake(0, self.f_heigh - _infoViewHeight, self.f_width, _infoViewHeight);
  58. _hiddenInfoViewRect = CGRectMake(0, self.f_heigh, self.f_width, _infoViewHeight);
  59. }
  60. - (void)buildView {
  61. [self addSubview:self.bgButton];
  62. [self addSubview:self.infoView];
  63. [self.infoView addSubview:self.tableView];
  64. [self.infoView addSubview:self.cancelButton];
  65. [self.bgButton setFrame:self.bounds];
  66. [self.infoView setFrame:_hiddenInfoViewRect];
  67. [self.cancelButton setFrame:CGRectMake(0, _infoViewHeight - CancelButtonHeight, self.f_width, CancelButtonHeight)];
  68. [self.tableView setFrame:CGRectMake(0, 0, self.f_width, _infoViewHeight - CancelButtonHeight)];
  69. if (_title.length > 0 && _title) {
  70. self.tableView.tableHeaderView = [self createHeaderView];
  71. }
  72. //设置
  73. //1.当其他按钮的数量大于默认最大数量 则开启滑动
  74. _tableView.scrollEnabled = (_otherButtonTitleArray.count > OtherButtonNumMax);
  75. //2.填充取消按钮text
  76. [self.cancelButton setTitle:_cancelTitle forState:UIControlStateNormal];
  77. //3.标题
  78. self.titleLabel.text = _title;
  79. [self show];
  80. }
  81. - (UIView *)createHeaderView {
  82. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.f_width, TitleViewHeight)];
  83. view.backgroundColor = [UIColor whiteColor];
  84. [view addSubview:self.titleLabel];
  85. self.titleLabel.frame = CGRectMake(0, 0, self.f_width, TitleViewHeight);
  86. [view addSubview:[UIView createLineRect:CGRectMake(0, TitleViewHeight - 0.5, self.f_width, 0.5)]];
  87. return view;
  88. }
  89. - (void)addAction {
  90. //确认 取消
  91. [self.cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  92. [self.bgButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  93. }
  94. - (void)cancelButtonAction:(id)sender {
  95. [self dismiss];
  96. if (self.actionSheetDidSelectedAtIndex) {
  97. self.actionSheetDidSelectedAtIndex(self->_otherButtonTitleArray.count, self->_selectedCellIndex);
  98. }
  99. }
  100. - (void)bgButtonAction:(id)sender {
  101. [self dismiss];
  102. }
  103. #pragma mark - Public
  104. //替换选中标示image
  105. - (void)setMarkImage:(UIImage *)markImage {
  106. _markImage = markImage;
  107. self.markImageView.image = markImage;
  108. }
  109. /**
  110. 显示ActionSheet
  111. */
  112. - (void)show {
  113. //添加
  114. [[UIApplication sharedApplication].delegate.window addSubview:self];
  115. [UIView animateWithDuration:.3 animations:^{
  116. self.infoView.frame = self->_showInfoViewRect;
  117. }];
  118. }
  119. /**
  120. 隐藏ActionSheet
  121. */
  122. - (void)dismiss {
  123. [UIView animateWithDuration:.3 animations:^{
  124. self.infoView.frame = self->_hiddenInfoViewRect;
  125. } completion:^(BOOL finished) {
  126. [self removeFromSuperview];
  127. }];
  128. }
  129. #pragma mark - UITableViewDelegate, UITableViewDataSource
  130. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  131. return _otherButtonTitleArray.count;
  132. }
  133. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  134. return _otherButtonHeight <= 0 ? OtherButtonHeight : _otherButtonHeight;
  135. }
  136. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  137. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
  138. cell.textLabel.text = _otherButtonTitleArray[indexPath.row];
  139. cell.textLabel.textColor = RGB(51, 51, 51);
  140. cell.textLabel.font = Font(16);
  141. if (_selectedCellIndex == indexPath.row) {
  142. [cell.contentView addSubview:self.markImageView];
  143. self.markImageView.frame = CGRectMake(self.f_width - 15 - 15, (OtherButtonHeight - 12) / 2, 15, 12);
  144. }
  145. return cell;
  146. }
  147. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  148. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  149. _selectedCellIndex = indexPath.row;
  150. [self.tableView reloadData];
  151. if (self.actionSheetDidSelectedAtIndex) {
  152. self.actionSheetDidSelectedAtIndex(indexPath.row, indexPath.row);
  153. }
  154. }
  155. #pragma mark - Getter
  156. - (UIButton *)bgButton {
  157. if (_bgButton == nil) {
  158. _bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
  159. _bgButton.backgroundColor = [UIColor blackColor];
  160. _bgButton.alpha = 0.4f;
  161. }
  162. return _bgButton;
  163. }
  164. - (UIView *)infoView {
  165. if (_infoView == nil) {
  166. _infoView = [UIView new];
  167. _infoView.backgroundColor = [UIColor whiteColor];
  168. }
  169. return _infoView;
  170. }
  171. - (UIButton *)cancelButton {
  172. if (_cancelButton == nil) {
  173. _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  174. _cancelButton.backgroundColor = Color_Button_Select;
  175. [_cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  176. }
  177. return _cancelButton;
  178. }
  179. - (UILabel *)titleLabel {
  180. if (_titleLabel == nil) {
  181. _titleLabel = [UILabel new];
  182. _titleLabel.font = Font(16);
  183. _titleLabel.textColor = RGB(153, 153, 153);
  184. _titleLabel.backgroundColor = [UIColor clearColor];
  185. _titleLabel.textAlignment = NSTextAlignmentCenter;
  186. }
  187. return _titleLabel;
  188. }
  189. - (UITableView *)tableView {
  190. if (_tableView == nil) {
  191. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  192. _tableView.delegate = self;
  193. _tableView.dataSource = self;
  194. _tableView.backgroundColor = Color_Background;
  195. _tableView.scrollEnabled = NO; //默认是不可以滚动
  196. [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
  197. }
  198. return _tableView;
  199. }
  200. - (UIImageView *)markImageView {
  201. if (_markImageView == nil) {
  202. _markImageView = [UIImageView new];
  203. _markImageView.image = DefaultMarkImageName;
  204. }
  205. return _markImageView;
  206. }
  207. @end