美文网首页
滚动的导航条(可以自己添加个数)

滚动的导航条(可以自己添加个数)

作者: Kloar | 来源:发表于2016-06-07 16:32 被阅读47次

    .h

    #import

    @interfaceYCSlideView :UIView

    @property(nonatomic,strong)NSArray* vcArray;

    - (instancetype)initWithFrame:(CGRect)frame WithViewControllers:(NSArray*)viewControllers;

    @end

    .m

    #import"YCSlideView.h"

    #define UIColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0green:(g)/255.0blue:(b)/255.0alpha:(a)]

    #define kWindowWidth([[UIScreen mainScreen] bounds].size.width)

    #define kWindowHeight([[UIScreen mainScreen] bounds].size.height)

    #define KTopViewHight50

    @interfaceYCSlideView()

    @property(nonatomic,strong)UIScrollView* bottomScrollView;

    @property(nonatomic,strong)UIView* topView;

    @property(nonatomic,strong)UIScrollView* topScrollView;

    @property(nonatomic,strong)UIView* slideView;

    @property(nonatomic,strong)NSMutableArray* btnArray;

    @end

    @implementationYCSlideView

    - (instancetype)initWithFrame:(CGRect)frame WithViewControllers:(NSArray*)viewControllers{

    if(self= [superinitWithFrame:frame]) {

    self.vcArray= viewControllers;

    }

    returnself;

    }

    - (void)setVcArray:(NSArray*)vcArray{

    _vcArray= vcArray;

    _btnArray= [NSMutableArrayarray];

    [selfconfingTopView];

    [selfconfigBottomView];

    }

    - (void)confingTopView{

    //按钮宽度

    CGFloatbuttonWight =kWindowWidth/_vcArray.count;

    //按钮高度

    CGFloatbuttonhight =KTopViewHight-4;

    CGRecttopViewFrame =CGRectMake(0,0,kWindowWidth,KTopViewHight

    );

    self.topView= [[UIViewalloc]initWithFrame:topViewFrame];

    [selfaddSubview:self.topView];

    self.slideView= [[UIViewalloc]initWithFrame:CGRectMake(0,KTopViewHight-3, buttonWight,3)];

    [_slideViewsetBackgroundColor:UIColorRGBA(239,93,58,1)];

    [_topViewaddSubview:self.slideView];

    //添加按钮

    for(inti =0; i

    UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(i * buttonWight,0, buttonWight, buttonhight)];

    UIButton*button = [[UIButtonalloc]initWithFrame:CGRectMake(0,0, buttonWight, buttonhight)];

    button.tag= i;

    NSString* buttonTitle =[self.vcArray[i]allKeys][0];

    [buttonsetTitle:buttonTitleforState:UIControlStateNormal];

    button.titleLabel.font= [UIFontsystemFontOfSize:17];

    [buttonsetTitleColor:UIColorRGBA(52,52,52,1)forState:UIControlStateNormal];

    if(i ==0) {

    [buttonsetTitleColor:UIColorRGBA(239,93,58,1)forState:UIControlStateNormal];

    }

    [buttonaddTarget:selfaction:@selector(tabButton:)forControlEvents:UIControlEventTouchUpInside];

    [viewaddSubview:button];

    [_btnArrayaddObject:button];

    [_topViewaddSubview:view];

    }

    UIView*lineView = [[UIViewalloc]initWithFrame:CGRectMake(0,KTopViewHight-1,kWindowWidth,1)];

    lineView.backgroundColor=UIColorRGBA(239,93,58,1);

    [_topViewaddSubview:lineView];

    }

    - (void)configBottomView{

    CGRectbottomScrollViewFrame =CGRectMake(0,KTopViewHight,kWindowWidth,kWindowHeight-KTopViewHight);

    self.bottomScrollView= [[UIScrollViewalloc]initWithFrame:bottomScrollViewFrame];

    [selfaddSubview:_bottomScrollView];

    for(inti =0; i

    CGRectVCFrame =CGRectMake(i *kWindowWidth,0,kWindowWidth, bottomScrollViewFrame.size.height);

    NSString* key = [self.vcArray[i]allKeys][0];

    UIViewController* vc =_vcArray[i][key] ;

    vc.view.frame= VCFrame;

    [self.bottomScrollViewaddSubview:vc.view];

    }

    self.bottomScrollView.contentSize=CGSizeMake(self.vcArray.count*kWindowWidth,0);

    self.bottomScrollView.pagingEnabled=YES;

    self.bottomScrollView.showsHorizontalScrollIndicator=NO;

    self.bottomScrollView.showsVerticalScrollIndicator=NO;

    self.bottomScrollView.directionalLockEnabled=YES;

    self.bottomScrollView.bounces=NO;

    self.bottomScrollView.delegate=self;

    }

    -(void)scrollViewDidScroll:(UIScrollView*)scrollView{

    CGRectframe =_slideView.frame;

    frame.origin.x= scrollView.contentOffset.x/_vcArray.count;

    _slideView.frame= frame;

    intpageNum = scrollView.contentOffset.x/kWindowWidth;

    for(UIButton* btnin_btnArray) {

    if(btn.tag== pageNum ) {

    [btnsetTitleColor:UIColorRGBA(239,93,58,1)forState:UIControlStateNormal];

    }else{

    [btnsetTitleColor:UIColorRGBA(52,52,52,1)forState:UIControlStateNormal];

    }

    }

    }

    -(void) tabButton: (id) sender{

    UIButton*button = sender;

    [buttonsetTitleColor:UIColorRGBA(239,93,58,1)forState:UIControlStateNormal];

    for(UIButton* btnin_btnArray) {

    if(button != btn ) {

    [btnsetTitleColor:UIColorRGBA(52,52,52,1)forState:UIControlStateNormal];

    }

    }

    [_bottomScrollViewsetContentOffset:CGPointMake(button.tag*kWindowWidth,0)animated:YES];

    }

    @end//

    //YCSlideView.m

    //youzer

    //

    //Created by王禹丞on 15/12/16.

    //Copyright © 2015年QXSX. All rights reserved.

    //

    #import"YCSlideView.h"

    #define UIColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0green:(g)/255.0blue:(b)/255.0alpha:(a)]

    #define kWindowWidth([[UIScreen mainScreen] bounds].size.width)

    #define kWindowHeight([[UIScreen mainScreen] bounds].size.height)

    #define KTopViewHight50

    @interfaceYCSlideView()

    @property(nonatomic,strong)UIScrollView* bottomScrollView;

    @property(nonatomic,strong)UIView* topView;

    @property(nonatomic,strong)UIScrollView* topScrollView;

    @property(nonatomic,strong)UIView* slideView;

    @property(nonatomic,strong)NSMutableArray* btnArray;

    @end

    @implementationYCSlideView

    - (instancetype)initWithFrame:(CGRect)frame WithViewControllers:(NSArray*)viewControllers{

    if(self= [superinitWithFrame:frame]) {

    self.vcArray= viewControllers;

    }

    returnself;

    }

    - (void)setVcArray:(NSArray*)vcArray{

    _vcArray= vcArray;

    _btnArray= [NSMutableArrayarray];

    [selfconfingTopView];

    [selfconfigBottomView];

    }

    - (void)confingTopView{

    //按钮宽度

    CGFloatbuttonWight =kWindowWidth/_vcArray.count;

    //按钮高度

    CGFloatbuttonhight =KTopViewHight-4;

    CGRecttopViewFrame =CGRectMake(0,0,kWindowWidth,KTopViewHight

    );

    self.topView= [[UIViewalloc]initWithFrame:topViewFrame];

    [selfaddSubview:self.topView];

    self.slideView= [[UIViewalloc]initWithFrame:CGRectMake(0,KTopViewHight-3, buttonWight,3)];

    [_slideViewsetBackgroundColor:UIColorRGBA(239,93,58,1)];

    [_topViewaddSubview:self.slideView];

    //添加按钮

    for(inti =0; i

    UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(i * buttonWight,0, buttonWight, buttonhight)];

    UIButton*button = [[UIButtonalloc]initWithFrame:CGRectMake(0,0, buttonWight, buttonhight)];

    button.tag= i;

    NSString* buttonTitle =[self.vcArray[i]allKeys][0];

    [buttonsetTitle:buttonTitleforState:UIControlStateNormal];

    button.titleLabel.font= [UIFontsystemFontOfSize:17];

    [buttonsetTitleColor:UIColorRGBA(52,52,52,1)forState:UIControlStateNormal];

    if(i ==0) {

    [buttonsetTitleColor:UIColorRGBA(239,93,58,1)forState:UIControlStateNormal];

    }

    [buttonaddTarget:selfaction:@selector(tabButton:)forControlEvents:UIControlEventTouchUpInside];

    [viewaddSubview:button];

    [_btnArrayaddObject:button];

    [_topViewaddSubview:view];

    }

    UIView*lineView = [[UIViewalloc]initWithFrame:CGRectMake(0,KTopViewHight-1,kWindowWidth,1)];

    lineView.backgroundColor=UIColorRGBA(239,93,58,1);

    [_topViewaddSubview:lineView];

    }

    - (void)configBottomView{

    CGRectbottomScrollViewFrame =CGRectMake(0,KTopViewHight,kWindowWidth,kWindowHeight-KTopViewHight);

    self.bottomScrollView= [[UIScrollViewalloc]initWithFrame:bottomScrollViewFrame];

    [selfaddSubview:_bottomScrollView];

    for(inti =0; i

    CGRectVCFrame =CGRectMake(i *kWindowWidth,0,kWindowWidth, bottomScrollViewFrame.size.height);

    NSString* key = [self.vcArray[i]allKeys][0];

    UIViewController* vc =_vcArray[i][key] ;

    vc.view.frame= VCFrame;

    [self.bottomScrollViewaddSubview:vc.view];

    }

    self.bottomScrollView.contentSize=CGSizeMake(self.vcArray.count*kWindowWidth,0);

    self.bottomScrollView.pagingEnabled=YES;

    self.bottomScrollView.showsHorizontalScrollIndicator=NO;

    self.bottomScrollView.showsVerticalScrollIndicator=NO;

    self.bottomScrollView.directionalLockEnabled=YES;

    self.bottomScrollView.bounces=NO;

    self.bottomScrollView.delegate=self;

    }

    -(void)scrollViewDidScroll:(UIScrollView*)scrollView{

    CGRectframe =_slideView.frame;

    frame.origin.x= scrollView.contentOffset.x/_vcArray.count;

    _slideView.frame= frame;

    intpageNum = scrollView.contentOffset.x/kWindowWidth;

    for(UIButton* btnin_btnArray) {

    if(btn.tag== pageNum ) {

    [btnsetTitleColor:UIColorRGBA(239,93,58,1)forState:UIControlStateNormal];

    }else{

    [btnsetTitleColor:UIColorRGBA(52,52,52,1)forState:UIControlStateNormal];

    }

    }

    }

    -(void) tabButton: (id) sender{

    UIButton*button = sender;

    [buttonsetTitleColor:UIColorRGBA(239,93,58,1)forState:UIControlStateNormal];

    for(UIButton* btnin_btnArray) {

    if(button != btn ) {

    [btnsetTitleColor:UIColorRGBA(52,52,52,1)forState:UIControlStateNormal];

    }

    }

    [_bottomScrollViewsetContentOffset:CGPointMake(button.tag*kWindowWidth,0)animated:YES];

    }

    @end

    sortViewController.m

    #import"sortViewController.h"

    #import"YCSlideView.h"

    #import"timeSortViewController.h"

    #import"SplendidViewSortController.h"

    #import"randomSortViewController.h"

    #define kWindowWidth([[UIScreen mainScreen] bounds].size.width)

    #define kWindowHeight([[UIScreen mainScreen] bounds].size.height)

    @interfacesortViewController()

    @end

    @implementationsortViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor=[UIColorwhiteColor];

    [selfsetNavAndView];

    }

    -(void)setNavAndView

    {

    NSArray*viewControllers =@[@{@"随机排序":[randomSortViewControllernew]},@{@"时间排序":[timeSortViewControllernew]},@{@"精彩排序":[SplendidViewSortControllernew]}];

    YCSlideView* view = [[YCSlideViewalloc]initWithFrame:CGRectMake(0,64,kWindowWidth,kWindowHeight-64)WithViewControllers:viewControllers];

    [self.viewaddSubview:view];

    // Do any additional setup after loading the view, typically from a nib.

    }

    - (BOOL)prefersStatusBarHidden

    {

    returnYES;

    }

    相关文章

      网友评论

          本文标题:滚动的导航条(可以自己添加个数)

          本文链接:https://www.haomeiwen.com/subject/wqjirttx.html