天天省钱快报

CCActionSheet.m 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // CCActionSheet.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/25.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "CCActionSheet.h"
  9. #import "CCActionSheetCell.h"
  10. #import "NSString+CCFunction.h"
  11. #define kWidth [UIScreen mainScreen].bounds.size.width
  12. #define kHeight [UIScreen mainScreen].bounds.size.height
  13. #define RGBColor(r,g,b,a) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a])
  14. #define BASE_COLOR RGBColor(242.0, 242.0, 242.0, 1.0)
  15. #define TABLEVIEW_BORDER_COLOR RGBColor(231.0, 231.0, 231.0, 1.0)
  16. #define ROW_HEIGHT 44
  17. #define CancelButtonTop 10
  18. #define iPhone_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
  19. #define KBottomMargin (iPhone_X ? 25.f : 0.f)
  20. @implementation CCActionSheet
  21. - (CCActionSheet *)initWithTitle:(NSString *)title delegate:(id<CCActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles
  22. {
  23. self = [super init];
  24. if (self)
  25. {
  26. self.userInteractionEnabled = YES;
  27. self.backgroundColor = [UIColor clearColor];
  28. self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  29. if (delegate) {
  30. self.delegate = delegate;
  31. }
  32. _title = title;
  33. _cancelButtonTitle = cancelButtonTitle;
  34. _destructiveButtonTitle = destructiveButtonTitle;
  35. _otherButtonTitles = [[NSMutableArray alloc] initWithArray:otherButtonTitles];
  36. if ([destructiveButtonTitle length]) {
  37. [_otherButtonTitles addObject:_destructiveButtonTitle];
  38. }
  39. if ([cancelButtonTitle length]) {
  40. [_otherButtonTitles addObject:cancelButtonTitle];
  41. self.cancelButtonIndex = [_otherButtonTitles count]-1;
  42. }
  43. _alphaView = [[UIView alloc] initWithFrame:self.bounds];
  44. _alphaView.backgroundColor = [UIColor blackColor];
  45. _alphaView.alpha = 0.0;
  46. [self addSubview:_alphaView];
  47. [self sendSubviewToBack:_alphaView];
  48. [_alphaView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
  49. [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];
  50. self.autoresizesSubviews = YES ;
  51. _alphaView.autoresizesSubviews = YES ;
  52. //取消
  53. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
  54. [_alphaView addGestureRecognizer:tapGesture];
  55. CGFloat addH = [_cancelButtonTitle length]?CancelButtonTop:0;
  56. CGFloat viewH = [self tableHeadHeight]+ROW_HEIGHT*[_otherButtonTitles count]+addH;
  57. _sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight, kWidth, viewH+KBottomMargin)];
  58. _sheetView.backgroundColor = BASE_COLOR;
  59. [self addSubview:_sheetView];
  60. [_sheetView addSubview:[self tableView]];
  61. [_sheetView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin];
  62. _sheetView.autoresizesSubviews = YES ;
  63. }
  64. return self;
  65. }
  66. -(void)showInView:(UIView *)view
  67. {
  68. [view addSubview:self];
  69. [UIView animateWithDuration:0.25
  70. animations:^{
  71. _alphaView.alpha = 0.5;
  72. [_sheetView setFrame:CGRectMake(0, kHeight-_sheetView.frame.size.height, kWidth, _sheetView.frame.size.height)];
  73. }];
  74. }
  75. -(void)tappedCancel
  76. {
  77. [UIView animateWithDuration:0.25
  78. animations:^{
  79. _alphaView.alpha = 0;
  80. [_sheetView setFrame:CGRectMake(0, kHeight, kWidth, _sheetView.frame.size.height)];
  81. }
  82. completion:^(BOOL finished) {
  83. [self removeFromSuperview];
  84. }];
  85. }
  86. #pragma mark - 初始化数据
  87. -(UITableView *)tableView
  88. {
  89. if (_tableView) {
  90. return _tableView;
  91. }
  92. _tableView = [[UITableView alloc] initWithFrame:_sheetView.bounds];
  93. _tableView.delegate = self;
  94. _tableView.dataSource = self;
  95. _tableView.alwaysBounceHorizontal = NO;
  96. _tableView.alwaysBounceVertical = NO;
  97. _tableView.showsHorizontalScrollIndicator = NO;
  98. _tableView.showsVerticalScrollIndicator = NO;
  99. _tableView.backgroundColor = [UIColor clearColor];
  100. _tableView.separatorColor = TABLEVIEW_BORDER_COLOR;
  101. _tableView.tableFooterView = [UIView new];
  102. [self addTableHead];
  103. [_tableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin];
  104. _tableView.autoresizesSubviews = YES ;
  105. if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
  106. [_tableView setSeparatorInset:UIEdgeInsetsZero];
  107. }
  108. if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
  109. [_tableView setLayoutMargins:UIEdgeInsetsZero];
  110. }
  111. return _tableView;
  112. }
  113. -(void)addTableHead
  114. {
  115. UIView *tableHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, [self tableHeadHeight])];
  116. tableHead.backgroundColor = [UIColor whiteColor];
  117. _tableView.tableHeaderView = tableHead;
  118. UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, kWidth-40, [_title sizeWithFont:[UIFont systemFontOfSize:14.0] maxSize:CGSizeMake(kWidth-40, kHeight)].height)];
  119. titleLab.text = _title;
  120. titleLab.textAlignment = NSTextAlignmentCenter;
  121. titleLab.textColor = [UIColor grayColor];
  122. titleLab.font = [UIFont systemFontOfSize:14.0];
  123. titleLab.numberOfLines = 0;
  124. [tableHead addSubview:titleLab];
  125. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, tableHead.frame.size.height, kWidth, 0.5)];
  126. line.backgroundColor = TABLEVIEW_BORDER_COLOR;
  127. [tableHead addSubview:line];
  128. }
  129. -(CGFloat)tableHeadHeight
  130. {
  131. CGFloat height = 0;
  132. if ([_title length]) {
  133. height += [_title sizeWithFont:[UIFont systemFontOfSize:14.0] maxSize:CGSizeMake(kWidth-40, kHeight)].height+40;
  134. }
  135. return height;
  136. }
  137. #pragma mark - tableView delegate/dataSource
  138. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  139. {
  140. return 1;
  141. }
  142. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  143. {
  144. return [_otherButtonTitles count];
  145. }
  146. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  147. {
  148. if ([_cancelButtonTitle length] && indexPath.row == [_otherButtonTitles count]-1) {
  149. return ROW_HEIGHT+CancelButtonTop;
  150. }
  151. return ROW_HEIGHT;
  152. }
  153. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. static NSString *identifier = @"cell";
  156. CCActionSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  157. if (cell == nil) {
  158. cell = [[CCActionSheetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  159. [cell buildUI];
  160. }
  161. cell.actionLabel.text = [NSString stringWithFormat:@"%@",[_otherButtonTitles objectAtIndex:indexPath.row]];
  162. if ([_destructiveButtonTitle length] && indexPath.row == [_otherButtonTitles count]-2) {
  163. cell.actionLabel.textColor = [UIColor redColor];
  164. }
  165. if ([_cancelButtonTitle length] && indexPath.row == [_otherButtonTitles count]-1) {
  166. cell.actionLabel.frame = CGRectMake(0, CancelButtonTop, kWidth, ROW_HEIGHT);
  167. }
  168. cell.backgroundColor = [UIColor clearColor];
  169. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  170. return cell;
  171. }
  172. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. if ([_cancelButtonTitle length] && indexPath.row == [_otherButtonTitles count]-1)
  175. {
  176. [self tappedCancel];
  177. // [self.delegate actionSheet:self clickedButtonAtIndex:indexPath.row];
  178. return;
  179. }
  180. [self.delegate actionSheet:self clickedButtonAtIndex:indexPath.row];
  181. [self tappedCancel];
  182. }
  183. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  184. {
  185. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  186. [cell setSeparatorInset:UIEdgeInsetsZero];
  187. }
  188. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  189. [cell setLayoutMargins:UIEdgeInsetsZero];
  190. }
  191. }
  192. @end