标题栏

作者: 一路向前1 | 来源:发表于2017-09-21 20:50 被阅读0次
    Untitled.gif
    
    #import "NewsViewController.h"#import "NewsDetailViewController.h"#import "NewsCollectionViewCell.h"#define BTN_W FIT_X(80) //标题的宽度//用于适配的宏#define  SCR_W  [UIScreen mainScreen].bounds.size.width    // 屏幕宽度#define  SCR_H  [UIScreen mainScreen].bounds.size.height  // 屏幕高度#define  FIT_X(x)  (SCR_W/375.*(x))    // 用于x轴适配 4.7#define  FIT_Y(y)  (SCR_H/667.*(y))    // 用于y轴适配@interface NewsViewController (){
    
    NSArray *_allTitles ; //所有的新闻标题;
    
    NSArray *_allDatas ; //所有的新闻数据 ;
    
    NSArray *_tableDatas ; //给每一个网络单元格中的表格视图加载数据的数组 ;
    
    }
    
    @property(nonatomic,strong)NewsViewController *detailVC ;
    
    @property(nonatomic,strong)UIScrollView *titleScrView; //标题滚动视图;
    
    @property(nonatomic,strong)UILabel *titleIndicateLabel; //标题下方的指示标签.
    
    @property(nonatomic,strong)UICollectionView *contentsColView; //新闻内容网络视图
    
    @end
    
    @implementation NewsViewController
    
    #pragma maek - 详情控制器实例化
    
    -(NewsViewController *)detailVC {
    
    if (!_detailVC) {
    
    _detailVC = [[NewsViewController alloc]init] ;
    
    }
    
    return _detailVC ;
    
    }
    
    //标题的滚动视图
    
    -(UIScrollView *)titleScrView {
    
    if (!_titleScrView) {
    
    _titleScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCR_W, FIT_Y(44))];
    
    _titleScrView.contentSize = CGSizeMake( BTN_W * _allTitles.count, FIT_Y(44));
    
    _titleScrView.showsHorizontalScrollIndicator = NO ;
    
    for (int i = 0; i < _allTitles.count; i ++) {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(i*BTN_W, 0, BTN_W, FIT_Y(44));
    
    [btn setTitle:_allTitles[i] forState:UIControlStateNormal];
    
    [btn setTitle:_allTitles[i] forState:UIControlStateSelected] ;
    
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    
    if (i == 0) {
    
    btn.selected = YES ;
    
    }
    
    btn.tag = i +100;
    
    [btn addTarget:self action:@selector(titleBtnHandle:) forControlEvents:UIControlEventTouchUpInside];
    
    [_titleScrView addSubview:btn];
    
    }
    
    }
    
    [_titleScrView addSubview:self.titleIndicateLabel];
    
    return _titleScrView;
    
    }
    
    //标题指示标签
    
    -(UILabel *)titleIndicateLabel {
    
    if (!_titleIndicateLabel) {
    
    _titleIndicateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, FIT_Y(42), BTN_W,FIT_Y(2))];
    
    _titleIndicateLabel.backgroundColor = [UIColor redColor] ;
    
    }
    
    return _titleIndicateLabel;
    
    }
    
    //新闻内容视图
    
    -(UICollectionView *)contentsColView {
    
    if (!_contentsColView) {
    
    //实例化网格布局对象
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    
    //设置每个单元格大小
    
    flow.itemSize =CGSizeMake(SCR_W, SCR_H-FIT_Y(44)) ;
    
    //设置最小行间距为0
    
    flow.minimumLineSpacing = 0 ;
    
    //设置最小列间距为0
    
    flow.minimumInteritemSpacing = 0 ;
    
    //设置滚动方形为水平滚动
    
    flow.scrollDirection  = UICollectionViewScrollDirectionHorizontal ;
    
    //实例化网络视图
    
    _contentsColView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, FIT_Y(44), SCR_W, SCR_H-FIT_Y(44)) collectionViewLayout:flow] ;
    
    _contentsColView.tag  = 444;
    
    _contentsColView.dataSource = self;
    
    _contentsColView.delegate = self;
    
    _contentsColView.pagingEnabled = YES;  //分页滚动效果
    
    //注册一个cell
    
    [_contentsColView registerClass:[NewsCollectionViewCell class] forCellWithReuseIdentifier:@"NewsCell"];
    
    }
    
    return _contentsColView;
    
    }
    
    -(void)titleBtnHandle:(UIButton *)btn {
    
    //让下方内容视图跟着变化
    
    [self.contentsColView scrollRectToVisible:CGRectMake((btn.tag-100)*SCR_W, 0, SCR_W, SCR_H-FIT_Y(44)) animated:YES];
    
    //    //选中button变红
    
    //    btn.selected = YES ;
    
    //
    
    //    [UIView animateWithDuration:0.185 animations:^{
    
    //        //移动指示标签 ;
    
    //        self.titleIndicateLabel.frame =CGRectMake((btn.tag-100)*BTN_W, FIT_Y(42), BTN_W, FIT_Y(2)) ;
    
    //    }] ;
    
    }
    
    #pragma mark -UICollectionViewDataSource
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    //多少标题就有多少分区
    
    return _allTitles.count ;
    
    }
    
    //单元格赋值
    
    -(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"NewsCell" ;
    
    NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath] ;
    
    if (cell == nil) {
    
    cell = [[NewsCollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H - FIT_Y(44))];
    
    }
    
    cell.newsTables.dataSource = self ;
    
    cell.newsTables.delegate = self;
    
    return cell ;
    
    }
    
    #pragma -mark -UIcollectionViewDelegate
    
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    //如果内容视图滚动,则执行if中的代码
    
    if (scrollView.tag == 444) {
    
    int index = scrollView.contentOffset.x/SCR_W ;
    
    //(1)改button
    
    for (int i = 0; i<_allTitles.count; i++) {
    
    UIButton *btn =(UIButton *)[self.titleScrView viewWithTag:i+100] ;
    
    btn.selected = NO ;
    
    if (index == i) {
    
    btn.selected = YES ;
    
    }
    
    }
    
    //(2)改标签指示的位置
    
    [UIView animateWithDuration:0.185 animations:^{
    
    self.titleIndicateLabel.frame =CGRectMake(BTN_W *index, FIT_Y(42), BTN_W, FIT_Y(2));
    
    }];
    
    //(3)让标题滚动视图到指定位置
    
    [self.titleScrView scrollRectToVisible:CGRectMake(BTN_W *index, 0, BTN_W, FIT_Y(44)) animated:YES] ;
    
    }
    
    }
    
    //将要出现cell时回调的方法
    
    - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld",indexPath.row);
    
    //(1)改变网格单元格中表格的视图的内容
    
    _tableDatas = _allDatas[indexPath.row] ;
    
    NewsCollectionViewCell *newsCell = (NewsCollectionViewCell *)cell ;
    
    [newsCell.newsTables reloadData];
    
    //    //让按钮变化
    
    //    for (int i = 0; i <_allTitles.count; i++) {
    
    //        UIButton *btn =[self.titleScrView viewWithTag:i+100];
    
    //        btn.selected = NO ;
    
    //        if (i == indexPath.row) {
    
    //            btn.selected = YES ;
    
    //        }
    
    //    }
    
    //    [UIView animateWithDuration:0.185 animations:^{
    
    //        //移动指示标签 ;
    
    //        self.titleIndicateLabel.frame =CGRectMake(indexPath.row*BTN_W, FIT_Y(42), BTN_W, FIT_Y(2)) ;
    
    //    }] ;
    
    //
    
    //    //选中button变红,其他的变为normal 颜色
    
    //    for (int i = 0; i < _allTitles.count;  i++) {
    
    //        UIButton *button = (UIButton *)[self.titleScrView viewWithTag:i+100];
    
    //        button.selected = NO ;
    
    //    }
    
    //(2)改变标题滚动视图的指示位置
    
    //    [self.titleScrView scrollRectToVisible:CGRectMake(BTN_W *indexPath.row, 0, BTN_W, FIT_Y(44)) animated:YES] ;
    
    //
    
    }
    
    #pragma -mark UITableViewDataSource
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _tableDatas.count ;
    
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *idenfifier = @"cell";
    
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:idenfifier];
    
    if (cell == nil) {
    
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idenfifier];
    
    }
    
    cell.textLabel.text = _tableDatas [indexPath.row];
    
    return cell ;
    
    }
    
    - (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    _allTitles = @[@"头条",@"新闻",@"体育",@"游戏",@"科技",@"财经",@"家具",@"健康",@"军事",@"娱乐"];
    
    _allDatas = @[@[@"墨西哥地震7.6级",@"🍎🍉",@"🍇🍑"],
    
    @[@"魏永峙被🐖干死",@"🍎🍉",@"🍇🍑"],
    
    @[@"跑步",@"游泳",@"跳高"],
    
    @[@"王者荣耀",@"英雄联盟",@"🍇🍑"],
    
    @[@"科技经贸",@"🍎🍉",@"🍇🍑"],
    
    @[@"月薪100000",@"🍎🍉",@"🍇🍑"],
    
    @[@"双人床",@"🍎🍉",@"🍇🍑"],
    
    @[@"健康",@"🍎🍉",@"🍇🍑"],
    
    @[@"ak-47",@"M16",@"暴力"],
    
    @[@"谁结婚",@"🍎🍉",@"🍇🍑"]];
    
    //用来接收真实的网络数据
    
    _tableDatas = _allDatas [0] ;
    
    UIView *view = [[UIView alloc]init];
    
    view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:view];
    
    [self.view addSubview:self.titleScrView];
    
    [self.view addSubview:self.contentsColView] ;
    
    }
    
    @end
    
    
    
    
    
    -(instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame] ;
    
    if (self) {
    
    self.newsTables = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H-FIT_Y(44)) style:UITableViewStylePlain];
    
    [self.contentView addSubview:self.newsTables] ;
    
    }
    
    return self;
    
    }
    
    

    相关文章

      网友评论

          本文标题:标题栏

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