猎豆优选

LBNavTabbarView.m 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // LBNavTabbarView.m
  3. // LBzzz
  4. //
  5. // Created by fighting on 17/2/7.
  6. // Copyright © 2017年 labi. All rights reserved.
  7. //
  8. #import "LBNavTabbarView.h"
  9. #import "LDSuperChildListVC.h"
  10. //#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  11. //#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
  12. #define FONTMAX 15.0
  13. #define FONTMIN 14.0
  14. #define PADDING 15.0
  15. @interface LBNavTabbarView ()<UIScrollViewDelegate>
  16. @property (nonatomic , strong)NSArray * vcArr;
  17. @property (nonatomic , strong)NSArray * viewArr;
  18. @property (nonatomic , strong)NSArray * titleArr;
  19. @property (nonatomic , strong)UIImageView * lineImgView;
  20. @property (nonatomic , strong)UIButton * currentBtn;
  21. @property (nonatomic , strong)UIScrollView * showScroll;
  22. @property (nonatomic , strong)UIScrollView * itemScroll;
  23. @property (nonatomic , assign) CGFloat lineHeight;
  24. @property (nonatomic , assign)NSInteger currentPage;
  25. @end
  26. @implementation LBNavTabbarView
  27. - (instancetype)initWithFrame:(CGRect)frame andVCArr:(NSArray<UIViewController *> *)vcArr andTitleArr:(NSArray <NSString *>*)titleArr lineHeight:(CGFloat)lineHeight
  28. {
  29. if (self = [super init]) {
  30. self.frame = frame;
  31. self.vcArr = [NSArray arrayWithArray:vcArr];
  32. self.titleArr = [NSArray arrayWithArray:titleArr];
  33. self.lineHeight = lineHeight;
  34. [self addSubview:self.itemScroll];
  35. }
  36. return self;
  37. }
  38. - (instancetype)initWithFrame:(CGRect)frame andViewArr:(NSArray<UIView *> *)viewArr andTitleArr:(NSArray <NSString *>*)titleArr lineHeight:(CGFloat)lineHeight
  39. {
  40. if (self = [super init]) {
  41. self.frame = frame;
  42. self.viewArr = [NSArray arrayWithArray:viewArr];
  43. self.titleArr = [NSArray arrayWithArray:titleArr];
  44. self.lineHeight = lineHeight;
  45. [self addSubview:self.itemScroll];
  46. }
  47. return self;
  48. }
  49. //初始化操作
  50. - (UIScrollView *)itemScroll
  51. {
  52. if (_itemScroll == nil) {
  53. _itemScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height)];
  54. _itemScroll.showsHorizontalScrollIndicator = NO;
  55. _itemScroll.showsVerticalScrollIndicator = NO;
  56. _itemScroll.alwaysBounceHorizontal = NO;
  57. _itemScroll.alwaysBounceVertical = YES;
  58. _itemScroll.bounces = NO;
  59. _itemScroll.backgroundColor = [UIColor whiteColor];
  60. [_itemScroll addSubview:self.lineImgView];
  61. NSInteger btnOffset = 0;
  62. for (int i = 0; i < self.titleArr.count; i++) {
  63. //添加上面的小标题按钮
  64. UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
  65. [btn setTitle:_titleArr[i] forState:UIControlStateNormal];
  66. if (self.titleColor) {
  67. [btn setTitleColor:self.titleColor forState:UIControlStateNormal];
  68. }else{
  69. [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
  70. }
  71. if (self.selectedTColor) {
  72. [btn setTitleColor:self.selectedTColor forState:UIControlStateSelected];
  73. }else{
  74. [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  75. }
  76. btn.titleLabel.font = [UIFont systemFontOfSize:FONTMIN];
  77. CGSize size = [self sizeOfLabelWithCustomMaxWidth:SCREEN_WIDTH systemFontSize:FONTMIN andFilledTextString:_titleArr[i]];
  78. float originX = i? PADDING*2+btnOffset:PADDING;
  79. btn.frame = CGRectMake(originX, 0, size.width, self.frame.size.height - self.lineHeight);
  80. btnOffset = CGRectGetMaxX(btn.frame);
  81. btn.titleLabel.textAlignment = NSTextAlignmentLeft;
  82. [btn addTarget:self action:@selector(changeSelectedItem:) forControlEvents:UIControlEventTouchUpInside];
  83. btn.tag = 111 + i;
  84. [_itemScroll addSubview:btn];
  85. //contentSize 等于按钮长度叠加
  86. //默认选中第一个按钮
  87. if (i == 0) {
  88. self.currentBtn = btn;
  89. btn.selected = YES;
  90. self.lineImgView.frame = CGRectMake(PADDING, self.frame.size.height - self.lineHeight, btn.frame.size.width, self.lineHeight);
  91. }
  92. //添加下面的显示View
  93. UIView * view = nil;
  94. if (self.vcArr) {
  95. LDSuperChildListVC * vc = self.vcArr[i];
  96. view = vc.view;
  97. }
  98. if (self.viewArr) {
  99. view= self.viewArr[i];
  100. }
  101. view.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.frame.origin.y - _itemScroll.frame.size.height);
  102. [self.showScroll addSubview:view];
  103. }
  104. _itemScroll.contentSize = CGSizeMake(btnOffset+PADDING, self.frame.size.height);
  105. }
  106. return _itemScroll;
  107. }
  108. //初始化操作
  109. - (UIScrollView *)showScroll
  110. {
  111. if (_showScroll == nil) {
  112. _showScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.frame.origin.y + self.itemScroll.frame.size.height, SCREEN_WIDTH, SCREEN_HEIGHT - self.frame.origin.y + self.itemScroll.frame.size.height)];
  113. _showScroll.contentSize = CGSizeMake(SCREEN_WIDTH * self.titleArr.count , SCREEN_HEIGHT - self.frame.origin.y + self.itemScroll.frame.size.height);
  114. _showScroll.showsHorizontalScrollIndicator = NO;
  115. _showScroll.showsVerticalScrollIndicator = NO;
  116. _showScroll.alwaysBounceHorizontal = NO;
  117. _showScroll.alwaysBounceVertical = YES;
  118. _showScroll.bounces = NO;
  119. _showScroll.pagingEnabled = YES;
  120. _showScroll.delegate = self;
  121. _showScroll.backgroundColor = [UIColor homeRedColor];
  122. [[self currentViewController].view addSubview:_showScroll];
  123. }
  124. return _showScroll;
  125. }
  126. //获取当前视图控制器
  127. - (UIViewController *)currentViewController
  128. {
  129. UIViewController *viewController = [[UIApplication sharedApplication].windows firstObject].rootViewController;
  130. while (viewController.presentedViewController) {
  131. viewController = viewController.presentedViewController;
  132. }
  133. return viewController;
  134. }
  135. #pragma mark - UIScrollViewDelegate
  136. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  137. {
  138. int index = scrollView.contentOffset.x / SCREEN_WIDTH;
  139. UIButton * currentButton = [self viewWithTag:111 + index];
  140. self.currentBtn.selected = NO;
  141. currentButton.selected = YES;
  142. self.currentBtn = currentButton;
  143. [UIView animateWithDuration:0.3 animations:^{
  144. if (index == 0) {
  145. self.lineImgView.frame = CGRectMake(PADDING, self.frame.size.height - self.lineHeight,currentButton.frame.size.width, self.lineHeight);
  146. }else{
  147. float offsetX = CGRectGetMinX(currentButton.frame);
  148. if (offsetX < SCREEN_WIDTH/2) {
  149. self.itemScroll.contentOffset = CGPointMake(0, 0);
  150. }else if (offsetX >= SCREEN_WIDTH/2 && offsetX <= self.itemScroll.contentSize.width - SCREEN_WIDTH/2) {
  151. self.itemScroll.contentOffset = CGPointMake(offsetX - SCREEN_WIDTH/2+ PADDING, 0);
  152. }else{
  153. self.itemScroll.contentOffset = CGPointMake(self.itemScroll.contentSize.width - SCREEN_WIDTH, 0);
  154. }
  155. self.lineImgView.frame = CGRectMake(CGRectGetMinX(currentButton.frame), self.frame.size.height - self.lineHeight, currentButton.frame.size.width, self.lineHeight);
  156. }
  157. }];
  158. }
  159. #pragma mark - 选项卡点击事件
  160. /**
  161. * 选项卡的点击
  162. *
  163. * @param currentButton 当前点击按钮
  164. */
  165. -(void)changeSelectedItem:(UIButton *)currentButton{
  166. self.currentBtn.selected = NO;
  167. currentButton.selected = YES;
  168. self.currentBtn = currentButton;
  169. NSInteger flag = currentButton.tag - 111;
  170. self.showScroll.contentOffset = CGPointMake(flag * SCREEN_WIDTH, 0);
  171. [UIView animateWithDuration:0.3 animations:^{
  172. if (flag == 0) {
  173. self.lineImgView.frame = CGRectMake(PADDING, self.frame.size.height - self.lineHeight,currentButton.frame.size.width, self.lineHeight);
  174. }else{
  175. float offsetX = CGRectGetMinX(currentButton.frame);
  176. if (offsetX < SCREEN_WIDTH/2) {
  177. self.itemScroll.contentOffset = CGPointMake(0, 0);
  178. }else if (offsetX >= SCREEN_WIDTH/2 && offsetX <= self.itemScroll.contentSize.width - SCREEN_WIDTH/2) {
  179. self.itemScroll.contentOffset = CGPointMake(offsetX - SCREEN_WIDTH/2+ PADDING, 0);
  180. }else{
  181. self.itemScroll.contentOffset = CGPointMake(self.itemScroll.contentSize.width - SCREEN_WIDTH, 0);
  182. }
  183. self.lineImgView.frame = CGRectMake(CGRectGetMinX(currentButton.frame), self.frame.size.height - self.lineHeight, currentButton.frame.size.width, self.lineHeight);
  184. }
  185. }];
  186. }
  187. //根据文字长度返回size
  188. - (CGSize)sizeOfLabelWithCustomMaxWidth:(CGFloat)width systemFontSize:(CGFloat)fontSize andFilledTextString:(NSString *)str{
  189. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, 0)];
  190. label.text = str;
  191. label.numberOfLines = 0;
  192. label.font = [UIFont systemFontOfSize:fontSize];
  193. //让label通过文字设置size
  194. [label sizeToFit];
  195. //获取label 的size
  196. CGSize size = label.frame.size;
  197. //返回出去
  198. return size;
  199. }
  200. //初始化操作
  201. - (UIImageView *)lineImgView {
  202. if (!_lineImgView) {
  203. _lineImgView = [[UIImageView alloc] init];
  204. //_lineImgView.image = [UIImage imageNamed:@"nar_bgbg"];
  205. if (self.downLineColor) {
  206. _lineImgView.backgroundColor = self.downLineColor;
  207. }else{
  208. _lineImgView.backgroundColor = [UIColor redColor];
  209. }
  210. }
  211. return _lineImgView;
  212. }
  213. @end