CSActionSheet.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // CSActionSheet.m
  3. //
  4. //
  5. // Created by e3mo on 15/7/7.
  6. // Copyright (c) 2015年 times. All rights reserved.
  7. //
  8. #import "CSActionSheet.h"
  9. #define SYS_CELL_HEIGHT 50.f
  10. #define SYS_CELL_LABEL_SIZE 15.f
  11. @implementation CSActionSheet
  12. - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles cancal:(NSString *)cancal normal_color:(UIColor *)normalColor highlighted_color:(UIColor *)color {
  13. return [self initWithFrame:frame titles:titles cancal:cancal normal_color:normalColor highlighted_color:color tips:nil tipsColor:nil cellBgColor:[UIColor whiteColor] cellLineColor:[UIColor colorWithRed:220.f/255.f green:220.f/255.f blue:220.f/255.f alpha:1]];
  14. }
  15. - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles cancal:(NSString *)cancal normal_color:(UIColor *)normalColor highlighted_color:(UIColor *)color tips:(NSString*)tips tipsColor:(UIColor*)tipsColor {
  16. return [self initWithFrame:frame titles:titles cancal:cancal normal_color:normalColor highlighted_color:color tips:tips tipsColor:tipsColor cellBgColor:[UIColor whiteColor] cellLineColor:[UIColor colorWithRed:220.f/255.f green:220.f/255.f blue:220.f/255.f alpha:1]];
  17. }
  18. - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles cancal:(NSString *)cancal normal_color:(UIColor *)normalColor highlighted_color:(UIColor *)color tips:(NSString *)tips tipsColor:(UIColor *)tipsColor cellBgColor:(UIColor *)bgColor cellLineColor:(UIColor *)lineColor {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.clipsToBounds = YES;
  22. isInAction = NO;
  23. titles_array = [[NSArray alloc] initWithArray:titles];
  24. [self setBackgroundColor:[UIColor clearColor]];
  25. close_btn = [UIButton buttonWithType:UIButtonTypeCustom];
  26. [close_btn setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  27. [close_btn setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]];
  28. [close_btn addTarget:self action:@selector(closeBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
  29. [self addSubview:close_btn];
  30. [self initShowView:cancal normal_color:normalColor highlighted_color:color tips:tips tipsColor:tipsColor bgColor:bgColor lineColor:lineColor];
  31. }
  32. return self;
  33. }
  34. - (void)initShowView:(NSString*)cancal normal_color:(UIColor *)normalColor highlighted_color:(UIColor *)color tips:(NSString *)tips tipsColor:(UIColor *)tipsColor bgColor:(UIColor*)bgColor lineColor:(UIColor*)lineColor {
  35. show_view = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 0)];
  36. [show_view setBackgroundColor:[UIColor clearColor]];
  37. [self addSubview:show_view];
  38. float picker_height = 0;
  39. if (tips && tips.length > 0) {
  40. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, show_view.frame.size.width-20, 0)];
  41. [label setText:tips];
  42. if (tipsColor) {
  43. [label setTextColor:tipsColor];
  44. }
  45. else {
  46. [label setTextColor:[UIColor grayColor]];
  47. }
  48. [label setTextAlignment:NSTextAlignmentCenter];
  49. label.numberOfLines = 0;
  50. [label setFont:[UIFont systemFontOfSize:13]];
  51. [label setBackgroundColor:[UIColor clearColor]];
  52. [label sizeToFit];
  53. CGRect frame = label.frame;
  54. frame.size.width = show_view.frame.size.width-20;
  55. label.frame = frame;
  56. UIImageView *tips_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, show_view.frame.size.width, label.frame.size.height+20)];
  57. [tips_bg setBackgroundColor:bgColor];
  58. [show_view addSubview:tips_bg];
  59. [tips_bg addSubview:label];
  60. picker_height += tips_bg.frame.size.height;
  61. UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, picker_height-1, show_view.frame.size.width, 1)];
  62. [line1 setBackgroundColor:[UIColor darkGrayColor]];
  63. [tips_bg addSubview:line1];
  64. }
  65. UIImageView *picker_bg = [[UIImageView alloc] initWithFrame:CGRectMake(10, picker_height, show_view.frame.size.width-20, titles_array.count*SYS_CELL_HEIGHT)];
  66. [picker_bg setBackgroundColor:bgColor];
  67. picker_bg.layer.cornerRadius = 8;
  68. picker_bg.layer.masksToBounds = YES;
  69. [show_view addSubview:picker_bg];
  70. for (int i=0; i<titles_array.count; i++) {
  71. NSString *title = [titles_array objectAtIndex:i];
  72. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  73. [btn setFrame:CGRectMake(10, picker_height+i*SYS_CELL_HEIGHT, picker_bg.frame.size.width, SYS_CELL_HEIGHT)];
  74. [btn setTitle:title forState:UIControlStateNormal];
  75. [btn setTitleColor:normalColor forState:UIControlStateNormal];
  76. if (color) {
  77. [btn setTitleColor:color forState:UIControlStateHighlighted];
  78. }
  79. [btn.titleLabel setFont:[UIFont systemFontOfSize:SYS_CELL_LABEL_SIZE]];
  80. [btn setBackgroundColor:[UIColor clearColor]];
  81. [btn addTarget:self action:@selector(sureBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
  82. [btn setTag:i+1];
  83. [show_view addSubview:btn];
  84. if (i != 0) {
  85. UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, i*SYS_CELL_HEIGHT-0.5, show_view.frame.size.width, 0.5)];
  86. [line1 setBackgroundColor:lineColor];
  87. [picker_bg addSubview:line1];
  88. }
  89. }
  90. picker_height += titles_array.count * SYS_CELL_HEIGHT;
  91. UIImageView *cancal_bg = [[UIImageView alloc] initWithFrame:CGRectMake(10, picker_height+5, show_view.frame.size.width-20, SYS_CELL_HEIGHT)];
  92. [cancal_bg setBackgroundColor:bgColor];
  93. cancal_bg.layer.cornerRadius = 8;
  94. cancal_bg.layer.masksToBounds = YES;
  95. [show_view addSubview:cancal_bg];
  96. cancal_btn = [UIButton buttonWithType:UIButtonTypeCustom];
  97. [cancal_btn setFrame:CGRectMake(10, picker_height+5, cancal_bg.frame.size.width, SYS_CELL_HEIGHT)];
  98. [cancal_btn setTitle:cancal forState:UIControlStateNormal];
  99. [cancal_btn setTitleColor:normalColor forState:UIControlStateNormal];
  100. if (color) {
  101. [cancal_btn setTitleColor:color forState:UIControlStateHighlighted];
  102. }
  103. [cancal_btn.titleLabel setFont:[UIFont systemFontOfSize:SYS_CELL_LABEL_SIZE]];
  104. [cancal_btn setBackgroundColor:[UIColor clearColor]];
  105. [cancal_btn addTarget:self action:@selector(sureBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
  106. cancal_btn.tag = 0;
  107. [show_view addSubview:cancal_btn];
  108. CGRect frame = show_view.frame;
  109. frame.size.height = picker_height + cancal_bg.frame.size.height + 5 + ShowDiff;
  110. show_view.frame = frame;
  111. }
  112. - (void)setCancalLabelColor:(UIColor*)color highlightedColor:(UIColor*)highColor {
  113. if (color) {
  114. [cancal_btn setTitleColor:color forState:UIControlStateNormal];
  115. }
  116. [cancal_btn setTitleColor:highColor forState:UIControlStateHighlighted];
  117. }
  118. - (void)sureBtnTouched:(id)sender {
  119. UIButton *btn = (UIButton*)sender;
  120. if (self.action) {
  121. self.action((int)btn.tag, self);
  122. }
  123. }
  124. - (void)closeBtnTouched:(id)sender {
  125. if (isInAction) {
  126. return;
  127. }
  128. [self hideView];
  129. }
  130. - (void)showView:(void (^)(int, id))action close:(void (^)(id))close {
  131. self.action = action;
  132. self.close = close;
  133. if (isInAction) {
  134. return;
  135. }
  136. isInAction = YES;
  137. [UIView animateWithDuration:0.3 animations:^{
  138. CGRect frame = show_view.frame;
  139. frame.origin.y = self.frame.size.height-frame.size.height;
  140. show_view.frame = frame;
  141. } completion:^(BOOL finished) {
  142. if (finished) {
  143. isInAction = NO;
  144. }
  145. }];
  146. }
  147. - (void)hideView {
  148. if (isInAction) {
  149. return;
  150. }
  151. isInAction = YES;
  152. [UIView animateWithDuration:0.3 animations:^{
  153. CGRect frame = show_view.frame;
  154. frame.origin.y = self.frame.size.height;
  155. show_view.frame = frame;
  156. } completion:^(BOOL finished) {
  157. if (finished) {
  158. isInAction = NO;
  159. if (self.close) {
  160. self.close(self);
  161. }
  162. }
  163. }];
  164. }
  165. - (BOOL)viewIsInAction {
  166. return isInAction;
  167. }
  168. @end