SectionChooseView.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 = NewWhiteColor;
  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. UIImageView *imgae = [UIImageView new];
  37. [imgae setImage:NewImageNamed(@"pp")];
  38. imgae.userInteractionEnabled = YES;
  39. [self addSubview:imgae];
  40. imgae.sd_layout
  41. .leftEqualToView(self)
  42. .rightEqualToView(self)
  43. .topEqualToView(self)
  44. .bottomEqualToView(self);
  45. //
  46. self.clipsToBounds = YES;
  47. self.layer.borderColor = self.selectBackgroundColor.CGColor;
  48. CGFloat itemWidth = (SCREEN_WIDTH -100-((_titleArray.count-1) * 5)) / _titleArray.count;
  49. for (int i = 0; i < _titleArray.count; i ++) {
  50. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  51. button.frame = CGRectMake(i * (itemWidth )+68, 15, itemWidth, 30);
  52. button.clipsToBounds = YES;
  53. button.tag = CustomSegmentBtnTag + i;
  54. ViewBorderRadius(button, 0, 0.8, NewWhiteColor);
  55. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:_normalTitleFont color:_titleNormalColor];
  56. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:_selectTitleFont color:_titleSelectColor];
  57. [button setBackgroundImage:[self createImageWithColor:_normalBackgroundColor] forState:UIControlStateNormal];
  58. [button setBackgroundImage:[self createImageWithColor:_normalBackgroundColor] forState:UIControlStateHighlighted];
  59. [button setBackgroundImage:[self createImageWithColor:_selectBackgroundColor] forState:UIControlStateSelected];
  60. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  61. [imgae addSubview:button];
  62. [imgae sendSubviewToBack:button];
  63. if (_selectIndex == i) {
  64. button.selected = YES;
  65. button.userInteractionEnabled = NO;
  66. }
  67. if (i == _titleArray.count - 1) continue;
  68. }
  69. }
  70. #pragma mark -分段按钮点击事件
  71. - (void)buttonClick:(UIButton *)button
  72. {
  73. if (button.tag - CustomSegmentBtnTag == _selectIndex) return;
  74. UIButton * oldButton = (UIButton *)[self viewWithTag:_selectIndex + CustomSegmentBtnTag];
  75. if (oldButton) {
  76. oldButton.selected = NO;
  77. oldButton.userInteractionEnabled = YES;
  78. }
  79. button.selected = YES;
  80. button.userInteractionEnabled = NO;
  81. _selectIndex = button.tag - CustomSegmentBtnTag;
  82. if (_delegate && [_delegate respondsToSelector:@selector(SectionSelectIndex:)]) {
  83. [_delegate SectionSelectIndex:_selectIndex];
  84. }
  85. }
  86. - (void)setCornerRadius:(CGFloat)cornerRadius
  87. {
  88. _cornerRadius = cornerRadius;
  89. self.layer.cornerRadius = cornerRadius;
  90. }
  91. - (void)setBorderWidth:(CGFloat)borderWidth
  92. {
  93. _borderWidth = borderWidth;
  94. self.layer.borderWidth = borderWidth;
  95. }
  96. #pragma mark -设置分段按钮背景颜色
  97. - (void)setNormalBackgroundColor:(UIColor *)normalBackgroundColor
  98. {
  99. if (normalBackgroundColor == _normalBackgroundColor) return;
  100. _normalBackgroundColor = normalBackgroundColor;
  101. for (int i = 0; i < self.titleArray.count; i ++) {
  102. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  103. if (button) {
  104. [button setBackgroundImage:[self createImageWithColor:normalBackgroundColor] forState:UIControlStateNormal];
  105. [button setBackgroundImage:[self createImageWithColor:normalBackgroundColor] forState:UIControlStateHighlighted];
  106. }
  107. }
  108. }
  109. - (void)setSelectBackgroundColor:(UIColor *)selectBackgroundColor
  110. {
  111. if (selectBackgroundColor == _selectBackgroundColor) return;
  112. _selectBackgroundColor = selectBackgroundColor;
  113. for (int i = 0; i < self.titleArray.count; i ++) {
  114. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  115. if (button) {
  116. [button setBackgroundImage:[self createImageWithColor:selectBackgroundColor] forState:UIControlStateSelected];
  117. }
  118. UIView * lineView = [self viewWithTag:CustomSegmentLineViewTag + i];
  119. if (lineView) {
  120. lineView.backgroundColor = selectBackgroundColor;
  121. }
  122. }
  123. self.layer.borderColor = selectBackgroundColor.CGColor;
  124. }
  125. #pragma mark -设置字体颜色
  126. - (void)setTitleNormalColor:(UIColor *)titleNormalColor
  127. {
  128. if (titleNormalColor == _titleNormalColor) return;
  129. _titleNormalColor = titleNormalColor;
  130. for (int i = 0; i < self.titleArray.count; i ++) {
  131. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  132. if (button) {
  133. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:_normalTitleFont color:titleNormalColor];
  134. }
  135. }
  136. }
  137. - (void)setTitleSelectColor:(UIColor *)titleSelectColor
  138. {
  139. if (titleSelectColor == _titleSelectColor) return;
  140. _titleSelectColor = titleSelectColor;
  141. for (int i = 0; i < self.titleArray.count; i ++) {
  142. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  143. if (button) {
  144. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:_selectTitleFont color:titleSelectColor];
  145. }
  146. }
  147. }
  148. #pragma mark -设置字体大小
  149. - (void)setNormalTitleFont:(CGFloat)normalTitleFont
  150. {
  151. if (normalTitleFont == _normalTitleFont) return;
  152. _normalTitleFont = normalTitleFont;
  153. for (int i = 0; i < self.titleArray.count; i ++) {
  154. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  155. if (button) {
  156. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:normalTitleFont color:_titleNormalColor];
  157. }
  158. }
  159. }
  160. - (void)setSelectTitleFont:(CGFloat)selectTitleFont
  161. {
  162. if (selectTitleFont == _selectTitleFont) return;
  163. _selectTitleFont = selectTitleFont;
  164. for (int i = 0; i < self.titleArray.count; i ++) {
  165. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  166. if (button) {
  167. [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:selectTitleFont color:_titleSelectColor];
  168. }
  169. }
  170. }
  171. - (void)setBtnTitleNormalOrSelectFont:(UIButton *)button buttonTitle:(NSString *)buttonTitle state:(UIControlState)state font:(CGFloat )font color:(UIColor *)color
  172. {
  173. NSDictionary * dic = @{
  174. NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:font],
  175. NSForegroundColorAttributeName : color
  176. };
  177. NSAttributedString * attributedTitle = [[NSAttributedString alloc] initWithString:buttonTitle attributes:dic];
  178. [button setAttributedTitle:attributedTitle forState:state];
  179. }
  180. #pragma mark -选中的item
  181. - (void)setSelectIndex:(NSInteger)selectIndex
  182. {
  183. if (selectIndex >= _titleArray.count || _selectIndex == selectIndex) {
  184. // 首次进入加载第一个界面通知
  185. [[NSNotificationCenter defaultCenter] postNotificationName:@"ABC" object:nil];
  186. };
  187. _selectIndex = selectIndex;
  188. for (int i = 0; i < _titleArray.count; i ++) {
  189. UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
  190. if (button) {
  191. button.selected = selectIndex == i ? YES : NO;
  192. button.userInteractionEnabled = selectIndex == i ? NO : YES;
  193. }
  194. }
  195. }
  196. - (UIImage *)createImageWithColor:(UIColor *)color
  197. {
  198. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  199. UIGraphicsBeginImageContext(rect.size);
  200. CGContextRef context = UIGraphicsGetCurrentContext();
  201. CGContextSetFillColorWithColor(context, [color CGColor]);
  202. CGContextFillRect(context, rect);
  203. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  204. UIGraphicsEndImageContext();
  205. return image;
  206. }
  207. - (void)dealloc {
  208. [[NSNotificationCenter defaultCenter] removeObserver:self];
  209. }
  210. @end