PageSwitchView.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // PagerView.m
  3. // IBOSSHSH
  4. //
  5. // Created by ssl on 2017/11/17.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. #import "PageSwitchView.h"
  9. #import "PageSwitchModel.h"
  10. @interface PageSwitchView()<UIScrollViewDelegate>{
  11. UIScrollView *scrollView;
  12. UIView *topView;
  13. UILabel *_sliderLabel;
  14. CGFloat oldContentOffSetX;
  15. }
  16. @end
  17. @implementation PageSwitchView
  18. /**
  19. 初始化
  20. @param frame <#frame description#>
  21. @return <#return value description#>
  22. */
  23. -(instancetype)initWithFrame:(CGRect)frame{
  24. self = [super initWithFrame:frame];
  25. self.translatesAutoresizingMaskIntoConstraints = NO;
  26. return self;
  27. }
  28. /**
  29. 初始化
  30. @param aDecoder <#aDecoder description#>
  31. @return <#return value description#>
  32. */
  33. -(instancetype)initWithCoder:(NSCoder *)aDecoder{
  34. self = [super initWithCoder:aDecoder];
  35. self.translatesAutoresizingMaskIntoConstraints = NO;
  36. return self;
  37. }
  38. /**
  39. 构建布局
  40. @param datas <#datas description#>
  41. */
  42. -(void) setDatas:(NSMutableArray *)datas{
  43. _datas = datas;
  44. topView = [[UIView alloc]init];
  45. topView.backgroundColor = [UIColor whiteColor];
  46. topView.translatesAutoresizingMaskIntoConstraints = NO;
  47. [self addSubview:topView];
  48. scrollView = [[UIScrollView alloc]init];
  49. scrollView.delegate = self;
  50. scrollView.backgroundColor = [UIColor whiteColor];
  51. scrollView.pagingEnabled = YES;
  52. scrollView.translatesAutoresizingMaskIntoConstraints = NO;
  53. scrollView.showsHorizontalScrollIndicator = NO;
  54. scrollView.showsVerticalScrollIndicator = NO;
  55. [self addSubview:scrollView];
  56. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(40)][scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(topView,scrollView)]];
  57. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(topView)]];
  58. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
  59. UIView *conview = [[UIView alloc] init];
  60. conview.translatesAutoresizingMaskIntoConstraints = NO;
  61. [scrollView addSubview:conview];
  62. [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[conview(width)]|" options:0 metrics:@{@"width":@(Screen_Width*datas.count)} views:NSDictionaryOfVariableBindings(conview)]];
  63. [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[conview(scrollView)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(conview,scrollView)]];
  64. for (NSInteger i = 0; i< datas.count; i++) {
  65. PageSwitchModel *model = datas[i];
  66. UIView *view = model.controller.view;
  67. view.translatesAutoresizingMaskIntoConstraints = NO;
  68. [conview addSubview:view];
  69. [conview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(wh)]" options:0 metrics:@{@"left":@(Screen_Width*i),@"wh":@(Screen_Width)} views:NSDictionaryOfVariableBindings(view)]];
  70. [conview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(conview)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view,conview)]];
  71. }
  72. [scrollView setContentOffset:CGPointMake((scrollView.frame.size.width)*_currentIndex, 0) animated:YES];
  73. for (int i = 0; i<datas.count; i++) {
  74. PageSwitchModel *model = datas[i];
  75. NSString *title = model.title;
  76. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  77. [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
  78. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  79. btn.frame = CGRectMake(Screen_Width/datas.count*i, 0, Screen_Width/datas.count, 40);
  80. btn.titleLabel.font = [UIFont systemFontOfSize:TitleFontOfSize];
  81. [btn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
  82. [btn setTitle:title forState:UIControlStateNormal];
  83. btn.tag = i;
  84. if(i == 0){
  85. btn.selected = YES;
  86. // 选中后的红线
  87. _sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x, 40-2, Screen_Width/datas.count, 2)];
  88. UIView *line = [[UIView alloc] init];
  89. line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
  90. line.backgroundColor = [UIColor redColor];
  91. [_sliderLabel addSubview:line];
  92. [topView addSubview:_sliderLabel];
  93. }
  94. [topView addSubview:btn];
  95. }
  96. }
  97. /**
  98. 按钮点击事件
  99. @param sender <#sender description#>
  100. */
  101. - (void)sliderAction:(UIButton *)sender{
  102. if (self.currentIndex == sender.tag) {
  103. return;
  104. }
  105. // 切换controller
  106. [self sliderAnimationWithTag:(int)sender.tag];
  107. [UIView animateWithDuration:0.3 animations:^{
  108. scrollView.contentOffset = CGPointMake(Screen_Width*(sender.tag), 0);
  109. UIView *line = _sliderLabel.subviews[0];
  110. line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
  111. } completion:^(BOOL finished) {
  112. }];
  113. }
  114. /**
  115. 滑动tag
  116. @param tag <#tag description#>
  117. */
  118. - (void)sliderAnimationWithTag:(int)tag{
  119. self.currentIndex = tag;
  120. for (int i = 0; i< topView.subviews.count; i++) {
  121. UIButton *sender = topView.subviews[i];
  122. if(![topView.subviews[i] isKindOfClass:[UILabel class]]){
  123. if(sender.tag != tag){
  124. sender.selected = NO;
  125. }else{
  126. sender.selected = YES;
  127. }
  128. }
  129. }
  130. }
  131. /**
  132. 开始滚动
  133. @param scrollView <#scrollView description#>
  134. */
  135. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  136. oldContentOffSetX = scrollView.contentOffset.x;
  137. }
  138. /**
  139. 滑动label位置
  140. @param scrollView <#scrollView description#>
  141. */
  142. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  143. //实时计算当前位置,实现和titleView上的按钮的联动
  144. [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
  145. CGFloat contentOffSetX = scrollView.contentOffset.x;
  146. CGFloat X = contentOffSetX/_datas.count;
  147. CGRect frame = _sliderLabel.frame;
  148. frame.origin.x = X;
  149. _sliderLabel.frame = frame;
  150. if(contentOffSetX - oldContentOffSetX > 0){
  151. if(fabs(contentOffSetX - oldContentOffSetX)>Screen_Width/2){
  152. [UIView animateWithDuration:0.2 animations:^{
  153. UIView *line = _sliderLabel.subviews[0];
  154. line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex+1])/2, 0, [self calculationWidth:_currentIndex+1], 2);
  155. } completion:^(BOOL finished) {
  156. }];
  157. }else{
  158. [UIView animateWithDuration:0.2 animations:^{
  159. UIView *line = _sliderLabel.subviews[0];
  160. line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
  161. } completion:^(BOOL finished) {
  162. }];
  163. }
  164. }else{
  165. if(fabs(contentOffSetX - oldContentOffSetX)>Screen_Width/2){
  166. [UIView animateWithDuration:0.2 animations:^{
  167. UIView *line = _sliderLabel.subviews[0];
  168. line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex-1])/2, 0, [self calculationWidth:_currentIndex-1], 2);
  169. } completion:^(BOOL finished) {
  170. }];
  171. }else{
  172. [UIView animateWithDuration:0.2 animations:^{
  173. UIView *line = _sliderLabel.subviews[0];
  174. line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
  175. } completion:^(BOOL finished) {
  176. }];
  177. }
  178. }
  179. }
  180. /**
  181. 滑动完成
  182. @param scrollView <#scrollView description#>
  183. */
  184. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  185. CGFloat contentOffSetX = scrollView.contentOffset.x;
  186. int index_ = contentOffSetX/Screen_Width;
  187. [self sliderAnimationWithTag:index_];
  188. }
  189. /**
  190. 计算标题文字的宽度
  191. @param index <#index description#>
  192. @return <#return value description#>
  193. */
  194. - (CGFloat) calculationWidth:(int) index{
  195. if(index==_datas.count){
  196. index =(int)[_datas count] -1;
  197. }else if(index < 0){
  198. index = 0;
  199. }
  200. NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
  201. PageSwitchModel *model = _datas[index];
  202. CGSize textSize = [model.title boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
  203. return textSize.width;
  204. }
  205. @end