| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- //
- // PagerView.m
- // IBOSSHSH
- //
- // Created by ssl on 2017/11/17.
- // Copyright © 2017年 elongtian. All rights reserved.
- //
- #import "PageSwitchView.h"
- #import "PageSwitchModel.h"
- @interface PageSwitchView()<UIScrollViewDelegate>{
- UIScrollView *scrollView;
- UIView *topView;
- UILabel *_sliderLabel;
- CGFloat oldContentOffSetX;
- }
- @end
- @implementation PageSwitchView
- /**
- 初始化
- @param frame <#frame description#>
- @return <#return value description#>
- */
- -(instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- self.translatesAutoresizingMaskIntoConstraints = NO;
- return self;
- }
- /**
- 初始化
- @param aDecoder <#aDecoder description#>
- @return <#return value description#>
- */
- -(instancetype)initWithCoder:(NSCoder *)aDecoder{
- self = [super initWithCoder:aDecoder];
- self.translatesAutoresizingMaskIntoConstraints = NO;
- return self;
- }
- /**
- 构建布局
- @param datas <#datas description#>
- */
- -(void) setDatas:(NSMutableArray *)datas{
- _datas = datas;
-
- topView = [[UIView alloc]init];
- topView.backgroundColor = [UIColor whiteColor];
- topView.translatesAutoresizingMaskIntoConstraints = NO;
-
- [self addSubview:topView];
- scrollView = [[UIScrollView alloc]init];
- scrollView.delegate = self;
- scrollView.backgroundColor = [UIColor whiteColor];
- scrollView.pagingEnabled = YES;
- scrollView.translatesAutoresizingMaskIntoConstraints = NO;
- scrollView.showsHorizontalScrollIndicator = NO;
- scrollView.showsVerticalScrollIndicator = NO;
- [self addSubview:scrollView];
- [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(40)][scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(topView,scrollView)]];
- [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(topView)]];
- [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]];
- UIView *conview = [[UIView alloc] init];
- conview.translatesAutoresizingMaskIntoConstraints = NO;
- [scrollView addSubview:conview];
- [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[conview(width)]|" options:0 metrics:@{@"width":@(Screen_Width*datas.count)} views:NSDictionaryOfVariableBindings(conview)]];
- [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[conview(scrollView)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(conview,scrollView)]];
- for (NSInteger i = 0; i< datas.count; i++) {
- PageSwitchModel *model = datas[i];
- UIView *view = model.controller.view;
- view.translatesAutoresizingMaskIntoConstraints = NO;
- [conview addSubview:view];
-
- [conview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[view(wh)]" options:0 metrics:@{@"left":@(Screen_Width*i),@"wh":@(Screen_Width)} views:NSDictionaryOfVariableBindings(view)]];
-
- [conview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(conview)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view,conview)]];
- }
-
- [scrollView setContentOffset:CGPointMake((scrollView.frame.size.width)*_currentIndex, 0) animated:YES];
-
- for (int i = 0; i<datas.count; i++) {
- PageSwitchModel *model = datas[i];
- NSString *title = model.title;
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
- [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- btn.frame = CGRectMake(Screen_Width/datas.count*i, 0, Screen_Width/datas.count, 40);
- btn.titleLabel.font = [UIFont systemFontOfSize:TitleFontOfSize];
- [btn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
- [btn setTitle:title forState:UIControlStateNormal];
- btn.tag = i;
- if(i == 0){
- btn.selected = YES;
- // 选中后的红线
- _sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x, 40-2, Screen_Width/datas.count, 2)];
- UIView *line = [[UIView alloc] init];
- line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
- line.backgroundColor = [UIColor redColor];
- [_sliderLabel addSubview:line];
- [topView addSubview:_sliderLabel];
- }
- [topView addSubview:btn];
- }
-
- }
- /**
- 按钮点击事件
-
- @param sender <#sender description#>
- */
- - (void)sliderAction:(UIButton *)sender{
- if (self.currentIndex == sender.tag) {
- return;
- }
- // 切换controller
- [self sliderAnimationWithTag:(int)sender.tag];
- [UIView animateWithDuration:0.3 animations:^{
- scrollView.contentOffset = CGPointMake(Screen_Width*(sender.tag), 0);
- UIView *line = _sliderLabel.subviews[0];
- line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
- } completion:^(BOOL finished) {
-
- }];
- }
- /**
- 滑动tag
-
- @param tag <#tag description#>
- */
- - (void)sliderAnimationWithTag:(int)tag{
- self.currentIndex = tag;
-
- for (int i = 0; i< topView.subviews.count; i++) {
- UIButton *sender = topView.subviews[i];
- if(![topView.subviews[i] isKindOfClass:[UILabel class]]){
- if(sender.tag != tag){
- sender.selected = NO;
- }else{
- sender.selected = YES;
- }
- }
- }
- }
- /**
- 开始滚动
- @param scrollView <#scrollView description#>
- */
- -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
- oldContentOffSetX = scrollView.contentOffset.x;
- }
- /**
- 滑动label位置
-
- @param scrollView <#scrollView description#>
- */
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
- //实时计算当前位置,实现和titleView上的按钮的联动
- [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
- CGFloat contentOffSetX = scrollView.contentOffset.x;
- CGFloat X = contentOffSetX/_datas.count;
- CGRect frame = _sliderLabel.frame;
- frame.origin.x = X;
- _sliderLabel.frame = frame;
- if(contentOffSetX - oldContentOffSetX > 0){
- if(fabs(contentOffSetX - oldContentOffSetX)>Screen_Width/2){
-
- [UIView animateWithDuration:0.2 animations:^{
- UIView *line = _sliderLabel.subviews[0];
- line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex+1])/2, 0, [self calculationWidth:_currentIndex+1], 2);
- } completion:^(BOOL finished) {
-
- }];
- }else{
- [UIView animateWithDuration:0.2 animations:^{
- UIView *line = _sliderLabel.subviews[0];
- line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
- } completion:^(BOOL finished) {
-
- }];
- }
- }else{
- if(fabs(contentOffSetX - oldContentOffSetX)>Screen_Width/2){
-
- [UIView animateWithDuration:0.2 animations:^{
- UIView *line = _sliderLabel.subviews[0];
- line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex-1])/2, 0, [self calculationWidth:_currentIndex-1], 2);
- } completion:^(BOOL finished) {
-
- }];
- }else{
- [UIView animateWithDuration:0.2 animations:^{
- UIView *line = _sliderLabel.subviews[0];
- line.frame = CGRectMake(((Screen_Width/_datas.count)- [self calculationWidth:_currentIndex])/2, 0, [self calculationWidth:_currentIndex], 2);
- } completion:^(BOOL finished) {
-
- }];
- }
-
- }
- }
- /**
- 滑动完成
-
- @param scrollView <#scrollView description#>
- */
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
- CGFloat contentOffSetX = scrollView.contentOffset.x;
- int index_ = contentOffSetX/Screen_Width;
- [self sliderAnimationWithTag:index_];
- }
- /**
- 计算标题文字的宽度
- @param index <#index description#>
- @return <#return value description#>
- */
- - (CGFloat) calculationWidth:(int) index{
- if(index==_datas.count){
- index =(int)[_datas count] -1;
- }else if(index < 0){
- index = 0;
- }
- NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
- PageSwitchModel *model = _datas[index];
- CGSize textSize = [model.title boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
- return textSize.width;
- }
- @end
|