CSActionPicker.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // CSActionPicker.m
  3. // NotePad
  4. //
  5. // Created by e3mo on 16/8/5.
  6. // Copyright © 2016年 e3mo. All rights reserved.
  7. //
  8. #import "CSActionPicker.h"
  9. #define SYS_MAX_HEIGHT 300.f
  10. #define SYS_CELL_HEIGHT 50.f
  11. #define SYS_CELL_LABEL_SIZE 16.f
  12. @implementation CSActionPicker
  13. - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles normal_color:(UIColor *)normalColor highlighted_color:(UIColor *)color {
  14. return [self initWithFrame:frame titles:titles normal_color:normalColor highlighted_color:color cellBgColor:[UIColor whiteColor] cellLineColor:[UIColor colorWithRed:220.f/255.f green:220.f/255.f blue:220.f/255.f alpha:1]];
  15. }
  16. - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles normal_color:(UIColor *)normalColor highlighted_color:(UIColor *)color cellBgColor:(UIColor *)bgColor cellLineColor:(UIColor *)lineColor {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.clipsToBounds = YES;
  20. isInAction = NO;
  21. titles_array = [[NSArray alloc] initWithArray:titles];
  22. [self setBackgroundColor:[UIColor clearColor]];
  23. close_btn = [UIButton buttonWithType:UIButtonTypeCustom];
  24. [close_btn setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  25. [close_btn setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]];
  26. [close_btn addTarget:self action:@selector(closeBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
  27. [self addSubview:close_btn];
  28. [self initShowViewWithNormal_color:normalColor highlighted_color:color bgColor:bgColor lineColor:lineColor];
  29. }
  30. return self;
  31. }
  32. - (void)initShowViewWithNormal_color:(UIColor *)normalColor highlighted_color:(UIColor *)color bgColor:(UIColor*)bgColor lineColor:(UIColor*)lineColor {
  33. show_view = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 0)];
  34. [show_view setBackgroundColor:[UIColor clearColor]];
  35. [self addSubview:show_view];
  36. float picker_height = 0;
  37. if (titles_array.count > 6) {
  38. UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, show_view.frame.size.width, SYS_MAX_HEIGHT)];
  39. [scrollview setBackgroundColor:bgColor];
  40. [scrollview setScrollEnabled:YES];//能否滑动
  41. [scrollview setShowsHorizontalScrollIndicator:NO];
  42. [scrollview setShowsVerticalScrollIndicator:YES];
  43. [scrollview setPagingEnabled:NO];//设置是否按页翻动
  44. [scrollview setBounces:NO];//设置是否反弹
  45. [scrollview setIndicatorStyle:UIScrollViewIndicatorStyleDefault];//设置风格
  46. [scrollview setDirectionalLockEnabled:NO];//设置是否同时运动
  47. UIView *sc_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollview.frame.size.width, titles_array.count*SYS_CELL_HEIGHT)];
  48. [sc_view setBackgroundColor:[UIColor clearColor]];
  49. for (int i=0; i<titles_array.count; i++) {
  50. NSString *title = [titles_array objectAtIndex:i];
  51. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  52. [btn setFrame:CGRectMake(0, picker_height+i*SYS_CELL_HEIGHT, scrollview.frame.size.width, SYS_CELL_HEIGHT)];
  53. [btn setTitle:title forState:UIControlStateNormal];
  54. [btn setTitleColor:normalColor forState:UIControlStateNormal];
  55. if (color) {
  56. [btn setTitleColor:color forState:UIControlStateHighlighted];
  57. }
  58. [btn.titleLabel setFont:[UIFont systemFontOfSize:SYS_CELL_LABEL_SIZE]];
  59. [btn setBackgroundColor:[UIColor clearColor]];
  60. [btn addTarget:self action:@selector(sureBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
  61. [btn setTag:i];
  62. [sc_view addSubview:btn];
  63. if (i != 0) {
  64. UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, i*SYS_CELL_HEIGHT-0.5, show_view.frame.size.width, 1)];
  65. [line1 setBackgroundColor:lineColor];
  66. [sc_view addSubview:line1];
  67. }
  68. }
  69. [scrollview addSubview:sc_view];
  70. [scrollview setContentSize:CGSizeMake(sc_view.frame.size.width, sc_view.frame.size.height)];//设置滑动范围
  71. [show_view addSubview:scrollview];
  72. picker_height += SYS_MAX_HEIGHT;
  73. }
  74. else {
  75. UIImageView *picker_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, picker_height, show_view.frame.size.width, titles_array.count*SYS_CELL_HEIGHT)];
  76. [picker_bg setBackgroundColor:bgColor];
  77. [show_view addSubview:picker_bg];
  78. for (int i=0; i<titles_array.count; i++) {
  79. NSString *title = [titles_array objectAtIndex:i];
  80. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  81. [btn setFrame:CGRectMake(0, picker_height+i*SYS_CELL_HEIGHT, picker_bg.frame.size.width, SYS_CELL_HEIGHT)];
  82. [btn setTitle:title forState:UIControlStateNormal];
  83. [btn setTitleColor:normalColor forState:UIControlStateNormal];
  84. if (color) {
  85. [btn setTitleColor:color forState:UIControlStateHighlighted];
  86. }
  87. [btn.titleLabel setFont:[UIFont systemFontOfSize:SYS_CELL_LABEL_SIZE]];
  88. [btn setBackgroundColor:[UIColor clearColor]];
  89. [btn addTarget:self action:@selector(sureBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
  90. [btn setTag:i];
  91. [show_view addSubview:btn];
  92. if (i != 0) {
  93. UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, i*SYS_CELL_HEIGHT-0.5, show_view.frame.size.width, 1)];
  94. [line1 setBackgroundColor:lineColor];
  95. [picker_bg addSubview:line1];
  96. }
  97. }
  98. picker_height += titles_array.count * SYS_CELL_HEIGHT;
  99. }
  100. CGRect frame = show_view.frame;
  101. frame.size.height = picker_height;
  102. frame.origin.y = -frame.size.height;
  103. show_view.frame = frame;
  104. }
  105. - (void)sureBtnTouched:(id)sender {
  106. UIButton *btn = (UIButton*)sender;
  107. if (self.action) {
  108. self.action((int)btn.tag, self);
  109. }
  110. }
  111. - (void)closeBtnTouched:(id)sender {
  112. if (isInAction) {
  113. return;
  114. }
  115. [self hideView];
  116. }
  117. - (void)showView:(void (^)(int, id))action close:(void (^)(id))close {
  118. if (isInAction) {
  119. return;
  120. }
  121. self.action = action;
  122. self.close = close;
  123. isInAction = YES;
  124. [UIView animateWithDuration:0.3 animations:^{
  125. CGRect frame = show_view.frame;
  126. frame.origin.y = 0;
  127. show_view.frame = frame;
  128. } completion:^(BOOL finished) {
  129. if (finished) {
  130. isInAction = NO;
  131. }
  132. }];
  133. }
  134. - (void)hideView {
  135. if (isInAction) {
  136. return;
  137. }
  138. isInAction = YES;
  139. [UIView animateWithDuration:0.3 animations:^{
  140. CGRect frame = show_view.frame;
  141. frame.origin.y = -show_view.frame.size.height;
  142. show_view.frame = frame;
  143. } completion:^(BOOL finished) {
  144. if (finished) {
  145. isInAction = NO;
  146. if (self.close) {
  147. self.close(self);
  148. }
  149. }
  150. }];
  151. }
  152. - (BOOL)viewIsInAction {
  153. return isInAction;
  154. }
  155. @end