// // BaseSlideViewController.m // SlideViewController // // Created by 周智勇 on 17/2/27. // Copyright © 2017年 周智勇. All rights reserved. // #import "BaseSlideViewController.h" #define ScreenW [UIScreen mainScreen].bounds.size.width #define ScreenH [UIScreen mainScreen].bounds.size.height #define RGBColor(r,g,b) RGBAColor(r,g,b,1.0) #define RGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a] static CGFloat const titleH = 41;/** 文字高度 */ static CGFloat const MaxScale = 1.1;/** 选中文字放大 */ @interface BaseSlideViewController () /** 控制器scrollView */ @property (nonatomic, strong) UIScrollView *contentScrollView; /** 标签文字 */ //@property (nonatomic ,strong) NSArray * titlesArr; /** 标签按钮 */ @property (nonatomic, strong) NSMutableArray *buttons; /** 选中的按钮 */ @property (nonatomic ,strong) UIButton * selectedBtn; /** 选中的按钮背景图 */ @property (nonatomic ,strong) UIImageView * imageBackView; /** 选中的按钮下边的线条 */ @property (nonatomic, strong)UIView * lineView; /** 文字scrollView */ @property (nonatomic, strong) UIScrollView *titleScrollView; @end @implementation BaseSlideViewController #pragma mark lazy loading - (NSMutableArray *)buttons { if (!_buttons) { _buttons = [NSMutableArray array]; } return _buttons; } - (void)updateSuperTitleAry{ //外部更新标签的时候,先要移除之前添加的控制器和视图 // [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // [obj removeFromSuperview]; // }]; [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [obj removeFromParentViewController]; }]; [self addChildViewController]; /** 添加子控制器视图 */ [self setTitleScrollView]; /** 添加文字标签 */ [self setContentScrollView]; /** 添加scrollView */ [self setupTitle]; /** 设置标签按钮 文字 背景图 */ self.automaticallyAdjustsScrollViewInsets = NO; self.contentScrollView.contentSize = CGSizeMake(self.titlesArr.count * ScreenW, 0); self.contentScrollView.pagingEnabled = YES; self.contentScrollView.showsHorizontalScrollIndicator = NO; self.contentScrollView.delegate = self; self.navBar.hidden=NO; self.navigationController.navigationBar.hidden=NO; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; } #pragma mark - PRIVATE -(void)addChildViewController{ for (int i = 0; i maxOffset) { offset = maxOffset; } [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES]; } -(void)setUpCurrentChildController:(NSInteger)index { } -(void)setUpOneChildController:(NSInteger)index{ CGFloat x = index * ScreenW; UIViewController *vc = self.childViewControllers[index]; [self setUpCurrentChildController:index]; if (vc.view.superview) { return; } vc.view.frame = CGRectMake(x, 0, ScreenW, ScreenH - self.contentScrollView.frame.origin.y); [self.contentScrollView addSubview:vc.view]; } #pragma mark - UIScrollView delegate -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSInteger i = self.contentScrollView.contentOffset.x / ScreenW; [self selectTitleBtn:self.buttons[i]]; [self setUpOneChildController:i]; //如果需要检测视图滑动了,可以使用下边的通知。但是.... // [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewControllerSlided" object:nil userInfo:@{@"currentIndex":@(i)}]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetX = scrollView.contentOffset.x; NSInteger leftIndex = offsetX / ScreenW; NSInteger rightIdex = leftIndex + 1; UIButton *leftButton = self.buttons[leftIndex]; UIButton *rightButton = nil; if (rightIdex < self.buttons.count) { rightButton = self.buttons[rightIdex]; } CGFloat scaleR = offsetX / ScreenW - leftIndex; CGFloat scaleL = 1 - scaleR; CGFloat transScale = MaxScale - 1; self.imageBackView.transform = CGAffineTransformMakeTranslation((offsetX*(self.titleScrollView.contentSize.width / self.contentScrollView.contentSize.width)), 0); leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1); rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1); //修改下划线的frame self.lineView.transform = CGAffineTransformMakeTranslation((offsetX*(self.titleScrollView.contentSize.width / self.contentScrollView.contentSize.width)), 0); } @end