SectionChooseView.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // SectionChooseView.m
  3. // CommunityService
  4. //
  5. // Created by lujh on 2017/3/8.
  6. // Copyright © 2017年 卢家浩. All rights reserved.
  7. //
  8. #define CustomSegmentBtnTag 888
  9. #define CustomSegmentLineViewTag 275
  10. #import "SectionChooseView.h"
  11. @interface SectionChooseView ()
  12. @property (nonatomic, strong) NSArray * titleArray;
  13. @end
  14. @implementation SectionChooseView
  15. - (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray
  16. {
  17. if (self = [super initWithFrame:frame]) {
  18. _titleArray = titleArray;
  19. if (_titleArray.count <= 0) return self;
  20. self.backgroundColor = NewNavigationColor;
  21. _normalBackgroundColor = [UIColor whiteColor];
  22. _selectBackgroundColor = [UIColor redColor];
  23. _titleNormalColor = [UIColor lightGrayColor];
  24. _titleSelectColor = [UIColor blueColor];
  25. _selectIndex = 0;
  26. _normalTitleFont = 14.0f;
  27. _selectTitleFont = 23.0f;
  28. // 初始化UI界面
  29. [self setUpSubviews];
  30. }
  31. return self;
  32. }
  33. #pragma mark -初始化UI界面
  34. - (void)setUpSubviews
  35. {
  36. self.clipsToBounds = YES;
  37. self.layer.borderColor = self.selectBackgroundColor.CGColor;
  38. CGFloat itemWidth = (SCREEN_WIDTH -136-((_titleArray.count-1) * 5)) / _titleArray.count;
  39. for (int i = 0; i < _titleArray.count; i ++) {
  40. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  41. button.frame = CGRectMake(i * (itemWidth +5)+68, 29, itemWidth, 27);
  42. button.clipsToBounds = YES;
  43. button.tag = CustomSegmentBtnTag + i;
  44. ViewBorderRadius(button, 3, 0.8, NewClearColor);
  45. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:_normalTitleFont color:_titleNormalColor];
  46. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:_selectTitleFont color:_titleSelectColor];
  47. [button setBackgroundImage:[self createImageWithColor:_normalBackgroundColor] forState:UIControlStateNormal];
  48. [button setBackgroundImage:[self createImageWithColor:_normalBackgroundColor] forState:UIControlStateHighlighted];
  49. [button setBackgroundImage:[self createImageWithColor:_selectBackgroundColor] forState:UIControlStateSelected];
  50. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  51. [self addSubview:button];
  52. [self sendSubviewToBack:button];
  53. if (_selectIndex == i) {
  54. button.selected = YES;
  55. button.userInteractionEnabled = NO;
  56. }
  57. if (i == _titleArray.count - 1) continue;
  58. }
  59. }
  60. #pragma mark -分段按钮点击事件
  61. - (void)buttonClick:(UIButton *)button
  62. {
  63. if (button.tag - CustomSegmentBtnTag == _selectIndex) return;
  64. UIButton * oldButton = (UIButton *)[self viewWithTag:_selectIndex + CustomSegmentBtnTag];
  65. if (oldButton) {
  66. oldButton.selected = NO;
  67. oldButton.userInteractionEnabled = YES;
  68. }
  69. button.selected = YES;
  70. button.userInteractionEnabled = NO;
  71. _selectIndex = button.tag - CustomSegmentBtnTag;
  72. if (_delegate && [_delegate respondsToSelector:@selector(SectionSelectIndex:)]) {
  73. [_delegate SectionSelectIndex:_selectIndex];
  74. }
  75. }
  76. - (void)setCornerRadius:(CGFloat)cornerRadius
  77. {
  78. _cornerRadius = cornerRadius;
  79. self.layer.cornerRadius = cornerRadius;
  80. }
  81. - (void)setBorderWidth:(CGFloat)borderWidth
  82. {
  83. _borderWidth = borderWidth;
  84. self.layer.borderWidth = borderWidth;
  85. }
  86. #pragma mark -设置分段按钮背景颜色
  87. - (void)setNormalBackgroundColor:(UIColor *)normalBackgroundColor
  88. {
  89. if (normalBackgroundColor == _normalBackgroundColor) return;
  90. _normalBackgroundColor = normalBackgroundColor;
  91. for (int i = 0; i < self.titleArray.count; i ++) {
  92. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  93. if (button) {
  94. [button setBackgroundImage:[self createImageWithColor:normalBackgroundColor] forState:UIControlStateNormal];
  95. [button setBackgroundImage:[self createImageWithColor:normalBackgroundColor] forState:UIControlStateHighlighted];
  96. }
  97. }
  98. }
  99. - (void)setSelectBackgroundColor:(UIColor *)selectBackgroundColor
  100. {
  101. if (selectBackgroundColor == _selectBackgroundColor) return;
  102. _selectBackgroundColor = selectBackgroundColor;
  103. for (int i = 0; i < self.titleArray.count; i ++) {
  104. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  105. if (button) {
  106. [button setBackgroundImage:[self createImageWithColor:selectBackgroundColor] forState:UIControlStateSelected];
  107. }
  108. UIView * lineView = [self viewWithTag:CustomSegmentLineViewTag + i];
  109. if (lineView) {
  110. lineView.backgroundColor = selectBackgroundColor;
  111. }
  112. }
  113. self.layer.borderColor = selectBackgroundColor.CGColor;
  114. }
  115. #pragma mark -设置字体颜色
  116. - (void)setTitleNormalColor:(UIColor *)titleNormalColor
  117. {
  118. if (titleNormalColor == _titleNormalColor) return;
  119. _titleNormalColor = titleNormalColor;
  120. for (int i = 0; i < self.titleArray.count; i ++) {
  121. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  122. if (button) {
  123. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:_normalTitleFont color:titleNormalColor];
  124. }
  125. }
  126. }
  127. - (void)setTitleSelectColor:(UIColor *)titleSelectColor
  128. {
  129. if (titleSelectColor == _titleSelectColor) return;
  130. _titleSelectColor = titleSelectColor;
  131. for (int i = 0; i < self.titleArray.count; i ++) {
  132. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  133. if (button) {
  134. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:_selectTitleFont color:titleSelectColor];
  135. }
  136. }
  137. }
  138. #pragma mark -设置字体大小
  139. - (void)setNormalTitleFont:(CGFloat)normalTitleFont
  140. {
  141. if (normalTitleFont == _normalTitleFont) return;
  142. _normalTitleFont = normalTitleFont;
  143. for (int i = 0; i < self.titleArray.count; i ++) {
  144. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  145. if (button) {
  146. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:normalTitleFont color:_titleNormalColor];
  147. }
  148. }
  149. }
  150. - (void)setSelectTitleFont:(CGFloat)selectTitleFont
  151. {
  152. if (selectTitleFont == _selectTitleFont) return;
  153. _selectTitleFont = selectTitleFont;
  154. for (int i = 0; i < self.titleArray.count; i ++) {
  155. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  156. if (button) {
  157. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:selectTitleFont color:_titleSelectColor];
  158. }
  159. }
  160. }
  161. - (void)setBtnTitleNormalOrSelectFont:(UIButton *)button buttonTitle:(NSString *)buttonTitle state:(UIControlState)state font:(CGFloat )font color:(UIColor *)color
  162. {
  163. NSDictionary * dic = @{
  164. NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:font],
  165. NSForegroundColorAttributeName : color
  166. };
  167. NSAttributedString * attributedTitle = [[NSAttributedString alloc] initWithString:buttonTitle attributes:dic];
  168. [button setAttributedTitle:attributedTitle forState:state];
  169. }
  170. #pragma mark -选中的item
  171. - (void)setSelectIndex:(NSInteger)selectIndex
  172. {
  173. if (selectIndex >= _titleArray.count || _selectIndex == selectIndex) {
  174. // 首次进入加载第一个界面通知
  175. [[NSNotificationCenter defaultCenter] postNotificationName:@"ABC" object:nil];
  176. };
  177. _selectIndex = selectIndex;
  178. for (int i = 0; i < _titleArray.count; i ++) {
  179. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  180. if (button) {
  181. button.selected = selectIndex == i ? YES : NO;
  182. button.userInteractionEnabled = selectIndex == i ? NO : YES;
  183. }
  184. }
  185. }
  186. - (UIImage *)createImageWithColor:(UIColor *)color
  187. {
  188. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  189. UIGraphicsBeginImageContext(rect.size);
  190. CGContextRef context = UIGraphicsGetCurrentContext();
  191. CGContextSetFillColorWithColor(context, [color CGColor]);
  192. CGContextFillRect(context, rect);
  193. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  194. UIGraphicsEndImageContext();
  195. return image;
  196. }
  197. - (void)dealloc {
  198. [[NSNotificationCenter defaultCenter] removeObserver:self];
  199. }
  200. @end