分享最热门的ios资讯
// // ViewController.m // // // Created by 雅致馨 on 16/6/3. // Copyright ? 2016年 雅致馨. All rights reserved. // #import "DCNavTabBarController.h" #import "HTMacro.h" @interface DCNavTabBarController ()@property (nonatomic, weak) UIButton *oldBtn; @property(nonatomic,strong) NSArray *VCArr; @property (nonatomic, weak) UIScrollView *contentView; @property (nonatomic, weak) UIScrollView *topBar; @property(nonatomic,assign) CGFloat btnW ; @property (nonatomic, weak) UIView *slider; @end @implementation DCNavTabBarController -(UIColor *)sliderColor { if(_sliderColor == nil) { _sliderColor = [UIColor colorWithRed:1.00 green:0.36 blue:0.25 alpha:1.00]; } return _sliderColor; } -(UIColor *)btnTextNomalColor { if(_btnTextNomalColor == nil) { _btnTextNomalColor = [UIColor colorWithWhite:0.205 alpha:1.000]; } return _btnTextNomalColor; } -(UIColor *)btnTextSeletedColor { if(_btnTextSeletedColor == nil) { _btnTextSeletedColor = [UIColor colorWithRed:1.00 green:0.36 blue:0.25 alpha:1.00]; } return _btnTextSeletedColor; } -(UIColor *)topBarColor { if(_topBarColor == nil) { _topBarColor = [UIColor whiteColor]; } return _topBarColor; } -(instancetype)initWithSubViewControllers:(NSArray *)subViewControllers { if(self = [super init]) { _VCArr = subViewControllers; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //添加上面的导航条 [self addTopBar]; //添加子控制器 [self addVCView]; //添加滑块 [self addSliderView]; } -(void)addSliderView { if(self.VCArr.count == 0) return; UIView *slider = [[UIView alloc]initWithFrame:CGRectMake(25,41,self.btnW - 50, 3)]; slider.backgroundColor = self.sliderColor; [self.topBar addSubview:slider]; self.slider = slider; } -(void)addTopBar { if(self.VCArr.count == 0) return; NSUInteger count = self.VCArr.count; UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)]; scrollView.backgroundColor = self.topBarColor; self.topBar = scrollView; self.topBar.bounces = NO; [self.view addSubview:self.topBar]; if(count <= 5) { self.btnW = SCREEN_WIDTH / count; } else { self.btnW = SCREEN_WIDTH / 5.0; } //添加button for (int i = 0; i =SCREEN_WIDTH && sender.tag != self.VCArr.count + 10000 - 1) { [UIView animateWithDuration:0.3 animations:^{ self.topBar.contentOffset = CGPointMake(maxX - SCREEN_WIDTH + self.btnW, -64); }]; }else if(maxX < SCREEN_WIDTH) { [UIView animateWithDuration:0.3 animations:^{ self.topBar.contentOffset = CGPointMake(0, 0); }]; } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { //滑动导航条 self.slider.frame = CGRectMake(scrollView.contentOffset.x / SCREEN_WIDTH *self.btnW + 25 , 41, self.btnW - 50, 3); } //判断是否切换导航条按钮的状态 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat offX = scrollView.contentOffset.x; int tag = (int)(offX /SCREEN_WIDTH + 0.5) + 10000; UIButton *btn = [self.view viewWithTag:tag]; if(tag != self.oldBtn.tag) { [self click:btn]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
用意志战胜身体的惰性!
本文由 姬長信 创作,文章地址:https://blog.isoyu.com/archives/1245.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 28, 2016 at 11:58 上午