// // PagerView.m // IBOSSHSH // // Created by ssl on 2017/11/17. // Copyright © 2017年 elongtian. All rights reserved. // #import "PageSwitchView.h" #import "PageSwitchModel.h" @interface PageSwitchView(){ 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 */ - (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