SegmentViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // SegmentViewController.m
  3. // CLSegment
  4. //
  5. // Created by CL on 2018/4/9.
  6. // Copyright © 2018年 cl. All rights reserved.
  7. //
  8. #import "SegmentViewController.h"
  9. #define HEADBTN_TAG 10000
  10. @interface SegmentViewController ()
  11. @property (nonatomic, strong) UIScrollView *headerView;
  12. @property (nonatomic, strong) UIScrollView *mainScrollView;
  13. @end
  14. @implementation SegmentViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. }
  18. - (void)initSegment:(CGFloat)height
  19. {
  20. [self addButtonInScrollHeader:_titleArray height:height];
  21. [self addContentViewScrollView:_subViewControllers height:height];
  22. }
  23. /*!
  24. * @brief 根据传入的title数组新建button显示在顶部scrollView上
  25. *
  26. * @param titleArray title数组
  27. */
  28. - (void)addButtonInScrollHeader:(NSArray *)titleArray height:(CGFloat)height
  29. {
  30. self.headerView.frame = CGRectMake(0, height, SCREEN_WIDTH, self.buttonHeight);
  31. if (_segmentHeaderType == 0) {
  32. self.headerView.contentSize = CGSizeMake(self.buttonWidth * titleArray.count, self.buttonHeight);
  33. }
  34. else {
  35. self.headerView.contentSize = CGSizeMake(SCREEN_WIDTH, self.buttonHeight);
  36. }
  37. [self.view addSubview:self.headerView];
  38. for (NSInteger index = 0; index < titleArray.count; index++) {
  39. UIButton *segmentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  40. segmentBtn.frame = CGRectMake(self.buttonWidth * index, 0, self.buttonWidth, self.buttonHeight);
  41. [segmentBtn setTitle:titleArray[index] forState:UIControlStateNormal];
  42. segmentBtn.titleLabel.font = [UIFont systemFontOfSize:self.fontSize];
  43. segmentBtn.tag = index + HEADBTN_TAG;
  44. [segmentBtn setTitleColor:self.titleColor forState:UIControlStateNormal];
  45. [segmentBtn setTitleColor:self.titleSelectedColor forState:UIControlStateSelected];
  46. [segmentBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  47. [self.headerView addSubview:segmentBtn];
  48. if (index == 0) {
  49. segmentBtn.selected = YES;
  50. self.selectIndex = segmentBtn.tag;
  51. }
  52. }
  53. }
  54. /*!
  55. * @brief 根据传入的viewController数组,将viewController的view添加到显示内容的scrollView
  56. *
  57. * @param subViewControllers viewController数组
  58. */
  59. - (void)addContentViewScrollView:(NSArray *)subViewControllers height:(CGFloat)height
  60. {
  61. _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.buttonHeight + height , SCREEN_WIDTH, SCREEN_HEIGHT - height - self.buttonHeight)];
  62. _mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * subViewControllers.count, SCREEN_HEIGHT - height - self.buttonHeight);
  63. [_mainScrollView setScrollEnabled:NO];
  64. [_mainScrollView setShowsVerticalScrollIndicator:NO];
  65. [_mainScrollView setShowsHorizontalScrollIndicator:NO];
  66. _mainScrollView.bounces = NO;
  67. [self.view addSubview:_mainScrollView];
  68. [subViewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
  69. UIViewController *viewController = (UIViewController *)_subViewControllers[idx];
  70. viewController.view.frame = CGRectMake(idx * SCREEN_WIDTH, 0, SCREEN_WIDTH, _mainScrollView.frame.size.height);
  71. [_mainScrollView addSubview:viewController.view];
  72. [self addChildViewController:viewController];
  73. }];
  74. }
  75. - (void)addParentController:(UIViewController *)viewController
  76. {
  77. if ([viewController respondsToSelector:@selector(edgesForExtendedLayout)])
  78. {
  79. viewController.edgesForExtendedLayout = UIRectEdgeNone;
  80. }
  81. [viewController addChildViewController:self];
  82. [viewController.view addSubview:self.view];
  83. }
  84. #pragma mark 点击按钮方法
  85. - (void)btnClick:(UIButton *)button
  86. {
  87. [_mainScrollView scrollRectToVisible:CGRectMake((button.tag - HEADBTN_TAG) *SCREEN_WIDTH, 0, SCREEN_WIDTH, _mainScrollView.frame.size.height) animated:NO];
  88. [self didSelectSegmentIndex:button.tag];
  89. }
  90. /*!
  91. * @brief 设置顶部选中button下方线条位置
  92. *
  93. * @param index 第几个
  94. */
  95. - (void)didSelectSegmentIndex:(NSInteger)index
  96. {
  97. UIButton *btn = (UIButton *)[self.view viewWithTag:self.selectIndex];
  98. btn.selected = NO;
  99. self.selectIndex = index;
  100. UIButton *currentSelectBtn = (UIButton *)[self.view viewWithTag:index];
  101. currentSelectBtn.selected = YES;
  102. }
  103. #pragma mark - ScrollViewDelegate
  104. //- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  105. //{
  106. // if (scrollView.contentOffset.x<=960) {
  107. // if (scrollView == _mainScrollView) {
  108. // float xx = scrollView.contentOffset.x * (_buttonWidth / SCREEN_WIDTH) - _buttonWidth;
  109. // [_headerView scrollRectToVisible:CGRectMake(xx, 0, SCREEN_WIDTH, _headerView.frame.size.height) animated:YES];
  110. // NSInteger currentIndex = scrollView.contentOffset.x / SCREEN_WIDTH;
  111. // [self didSelectSegmentIndex:currentIndex + HEADBTN_TAG];
  112. // }
  113. // }else{
  114. // [scrollView setScrollEnabled:NO];
  115. // }
  116. //}
  117. //
  118. //- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
  119. //{
  120. // float xx = scrollView.contentOffset.x * (_buttonWidth / SCREEN_WIDTH) - _buttonWidth;
  121. // [_headerView scrollRectToVisible:CGRectMake(xx, 0, SCREEN_WIDTH, _headerView.frame.size.height) animated:YES];
  122. //}
  123. #pragma mark - setter/getter
  124. - (UIScrollView *)headerView
  125. {
  126. if (_headerView == nil) {
  127. _headerView = [[UIScrollView alloc] init];
  128. [_headerView setShowsVerticalScrollIndicator:NO];
  129. [_headerView setShowsHorizontalScrollIndicator:NO];
  130. _headerView.bounces = NO;
  131. _headerView.backgroundColor = self.headViewBackgroundColor;
  132. }
  133. return _headerView;
  134. }
  135. - (UIColor *)headViewBackgroundColor
  136. {
  137. if (_headViewBackgroundColor == nil) {
  138. _headViewBackgroundColor = UIColorHex(0xFFFFFF);
  139. }
  140. return _headViewBackgroundColor;
  141. }
  142. - (UIColor *)titleColor
  143. {
  144. if (_titleColor == nil) {
  145. _titleColor = UIColorHex(0x0A0A0A);
  146. }
  147. return _titleColor;
  148. }
  149. - (UIColor *)titleSelectedColor
  150. {
  151. if (_titleSelectedColor == nil) {
  152. _titleSelectedColor = UIColorHex(0x3979D3);
  153. }
  154. return _titleSelectedColor;
  155. }
  156. - (CGFloat)fontSize
  157. {
  158. if (_fontSize == 0) {
  159. _fontSize = 15.f;
  160. }
  161. return _fontSize;
  162. }
  163. - (CGFloat)buttonWidth
  164. {
  165. if (_buttonWidth == 0) {
  166. _buttonWidth = SCREEN_WIDTH / 4;
  167. }
  168. return _buttonWidth;
  169. }
  170. - (CGFloat)buttonHeight
  171. {
  172. if (_buttonHeight == 0) {
  173. _buttonHeight = 50.f;
  174. }
  175. return _buttonHeight;
  176. }
  177. - (CGFloat)bottomLineHeight
  178. {
  179. if (_bottomLineHeight == 0) {
  180. _bottomLineHeight = 1.f;
  181. }
  182. return _bottomLineHeight;
  183. }
  184. - (CGFloat)bottomCount
  185. {
  186. if (_bottomCount == 0) {
  187. _bottomCount = 1;
  188. }
  189. return _bottomCount;
  190. }
  191. - (UIColor *)bottomLineColor
  192. {
  193. if (_bottomLineColor == nil) {
  194. _bottomLineColor = [UIColor whiteColor];
  195. }
  196. return _bottomLineColor;
  197. }
  198. @end