iOS实现自定义导航

#import "ViewController.h"

#import "SubViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    //存放视图控制器数据源

    NSMutableArray *_vcArray;

    //存放按钮的数组

    NSMutableArray * _btnsArray;

    UITableView *_tb;

    //头部滚动视图

    UIScrollView *_topScrollView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self setupTopScroll];

    [self setupVCArray];

    [self setupTableView];

}

#define  SWIDTH [UIScreen mainScreen].bounds.size.width

#define SHEIGHT [UIScreen mainScreen].bounds.size.height

-(void)setupTopScroll{

    //不让他自动调整scroll

    _btnsArray = [NSMutableArray array];

    self.automaticallyAdjustsScrollViewInsets = NO;

    _topScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SWIDTH, 30)];

    //[self.view addSubview:_topScrollView];

    self.navigationItem.titleView = _topScrollView;

    

    

    NSArray *titles = @[@"科技",@"博客",@"动态",@"新闻",

                        @"王大",@"王二",@"王三",@"王四",

                        @"王五",@"王六",@"王七",@"王八"

                        ];

    

    for(int i = 0;i < 12;i++){

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(i*SWIDTH/4, 0, SWIDTH/4, 30);

        [button setTitle:titles[i] forState:UIControlStateNormal];

        if (i == 0) {

            [button setTitleColor:[UIColor colorWithRed:0.8 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];

        }

        else{

            [button setTitleColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0] forState:UIControlStateNormal];}

        button.tag = 1000+i;

        [button addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventTouchUpInside];

        button.titleLabel.font = [UIFont systemFontOfSize:14];

        [_topScrollView addSubview:button];

        [_btnsArray addObject:button];

    }

    _topScrollView.contentSize = CGSizeMake(SWIDTH*3, 0);

   

}


//初始化数据源 数据源存放视图控制器

-(void)setupVCArray{

    _vcArray = [[NSMutableArray alloc]init];;

    for (NSInteger i = 0; i < 12; i++) {

        //此时subVCview还没有被创建出来,也即是subVC中的viewDidLoad方法不会被执行 如何使用到view viewDidLoad会被立刻执行

        SubViewController *subVC = [[SubViewController alloc]init];

        subVC.index = i;

        [_vcArray addObject:subVC];

    }

}


//初始化tableView

-(void)setupTableView{

    _tb= [[UITableView alloc]init];

    //逆时针旋转90 tableView变成横向滚动

    _tb.transform = CGAffineTransformMakeRotation(-M_PI_2);

    //旋转过后再去设置frame

    _tb.frame = CGRectMake(0, 94, SWIDTH, SHEIGHT-94);

    //关闭回弹

    _tb.bounces = NO;

    _tb.pagingEnabled = YES;

    //去除分割线

    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;

    //设置行高

    _tb.rowHeight = SWIDTH;

    //去除tableView的滚动条

    _tb.showsVerticalScrollIndicator = NO;

    _tb.delegate = self;

    _tb.dataSource = self;

    [self.view addSubview:_tb];

    [_tb registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

}


#pragma mark - tableViewDelegate


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _vcArray.count;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    //去除cell的选中样式

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.transform = CGAffineTransformMakeRotation(M_PI_2);

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

    label.text = @"zhehisdadasd";

    [cell.contentView addSubview:label];

    //布局cell

    UIViewController *vc = _vcArray[indexPath.row];

    vc.view.frame = cell.contentView.bounds;

    //先移除子视图,否则可能出现一个cell上出现了多个视图控制器的view

    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    

    

    [cell.contentView addSubview:vc.view];

    return cell;

}



//滚动中实时被调用

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

    //tableView 偏移量 横向过来了

    CGFloat offSetY = scrollView.contentOffset.y;

    //每一页偏移量 SWIDTH

    //相对位移CGFloat width = (NSInteger )offSetX%(NSInteger)SWIDTH;


    CGFloat ratio = (NSInteger )offSetY%(NSInteger)SWIDTH/SWIDTH;

    NSLog(@"%f",ratio);

    //当前的页数

    NSInteger curIndex = (offSetY+SWIDTH/2)/SWIDTH;

    NSLog(@"%ld",curIndex);

    //下一页

    //NSInteger nextIndex = ratio<0.5?curIndex+1:curIndex-1;

    

    if (offSetY != SWIDTH*curIndex) {

        NSInteger nextIndex= offSetY>curIndex*SWIDTH?curIndex+1:curIndex-1;

        if(curIndex >= nextIndex){

            curIndex = curIndex +nextIndex;

            nextIndex = curIndex -nextIndex;

            curIndex = curIndex -nextIndex;

            

        }

        UIButton *curBtn = _btnsArray[curIndex];

        UIButton *nextBtn = _btnsArray[nextIndex];

        [curBtn setTitleColor:[UIColor colorWithRed:0.8 green:0.8*ratio blue:0.8*ratio alpha:1.0] forState:UIControlStateNormal];

        [nextBtn setTitleColor:[UIColor colorWithRed:0.8 green:0.8*(1-ratio) blue:0.8*(1-ratio) alpha:1.0] forState:UIControlStateNormal];

        curBtn.transform = CGAffineTransformMakeScale(1+0.2-ratio*0.2, 1+0.2-ratio *0.2);

        nextBtn.transform = CGAffineTransformMakeScale(1+ratio*0.2, 1+ratio*0.2);

        

    }else{

        //翻页结束时获取当前页所对应的按钮

        UIButton *btn = _btnsArray[curIndex];

        //获取按钮中心点坐标

        //CGRectGetMaxX(btn.frame)-SWIDTH/2顶部scroll要偏移的大小

        //_topScrollView.contentSize.width - SWIDTH 顶部scrollviewx轴上偏移的最大值

        if ((CGRectGetMidX(btn.frame)-SWIDTH/2)>(_topScrollView.contentSize.width - SWIDTH) ) {

            [_topScrollView setContentOffset:CGPointMake(_topScrollView.contentSize.width - SWIDTH, 0) animated:YES];

        }else if(CGRectGetMidX(btn.frame)>SWIDTH/2){

             [_topScrollView setContentOffset:CGPointMake(CGRectGetMidX(btn.frame) -SWIDTH/2, 0) animated:YES];

            

        }else{

            [_topScrollView setContentOffset:CGPointMake(0, 0) animated:YES];

        }

     

    }

    

    //获取产生动画的button

 

}

//停止减速

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

    

}

//停止拖拽

//decelerateNOscrollView为静止 不会触发scrollViewDidEndDecelerate

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    

}


//-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

//    //即将拖拽结束

//}


#pragma mark - 点击事件

-(void)changePage:(UIButton*)button{

    //获取当前页的索引

    NSInteger index = _tb.contentOffset.y/SWIDTH;

    UIButton *curBtn = _btnsArray[index];

    [curBtn setTitleColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0] forState:UIControlStateNormal];

    

    [button setTitleColor:[UIColor colorWithRed:0.8 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];

    //tableView滚动到这个页面

    //btn在数组中的索引

    [_tb setContentOffset:CGPointMake(0,SWIDTH*(button.tag - 1000)) animated:NO];

    //_tb.contentOffset = CGPointMake(SWIDTH*(button.tag - 1000), 0);

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


你可能感兴趣的:(ios)