父子控制器的使用

//
//  ViewController.m
//  childDemo
//
//  Created by ios on 16/9/28.
//  Copyright © 2016年 ios. All rights reserved.
//

#import "ViewController.h"
#import "FisrstViewController.h"
#import "SecondViewController.h"
#import "MyCollectionViewCell.h"

#define NormalFont_size  16.0
#define SelectFont_size  22.0

static NSString *cellId = @"MyCell";

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    
    CGFloat btnW;

}

@property (nonatomic, strong)UIScrollView *topScrollerView;

@property (nonatomic, strong)UICollectionView *viewControllers;

@property (nonatomic, strong)NSArray *subVC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self prepareUI];
    [self createTopView];
    
}

#pragma  mark - 顶部滑动视图
- (void)createTopView {
    
    _topScrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 40)];
    _topScrollerView.backgroundColor = [UIColor orangeColor];
     //    _topScrollerView.delegate = self;
    [self.view addSubview:_topScrollerView];
    
     btnW = self.view.bounds.size.width / _subVC.count;

    _topScrollerView.contentSize = CGSizeMake(btnW * _subVC.count, 0);
    
    //创建标题按钮
    for (int i = 0; i < _subVC.count; i++) {
        
        UIButton *topBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
        topBtn.frame = CGRectMake(i * btnW, 4, btnW, _topScrollerView.frame.size.height - 8);
        topBtn.tag = i + 2016;
        [topBtn addTarget:self action:@selector(topBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [topBtn setTitle:@"炉石" forState:UIControlStateNormal];
        [topBtn setTitle:@"炉石" forState:UIControlStateSelected];
        
        //默认选中第一个频道
        if (i == 0) {
         
        topBtn.selected = YES;
        [self changeStatus:topBtn];
          
            
        }
        

        [_topScrollerView addSubview:topBtn];
    }
    
}


#pragma  mark - 容器视图
- (void)prepareUI{
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    flow.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 64 - 40);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //容器视图装的是UIViewController
    _viewControllers = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + 40, self.view.bounds.size.width, self.view.bounds.size.height-64 - 40)
                                         collectionViewLayout:flow];
    _viewControllers.delegate = self;
    _viewControllers.dataSource = self;
    _viewControllers.pagingEnabled = YES;
    _viewControllers.showsHorizontalScrollIndicator = NO;
    //注册
    [_viewControllers registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:cellId];
    [self.view addSubview:_viewControllers];
    
    
    FisrstViewController *firstVC = [[FisrstViewController alloc]init];
    firstVC.view.backgroundColor = [UIColor blueColor];
    
    
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    secondVC.view.backgroundColor = [UIColor cyanColor];
    
    [self addChildViewController:firstVC];
    [self addChildViewController:secondVC];
    
    _subVC = @[firstVC, secondVC];
    
    

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    return _subVC.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    
    UIViewController *VC = self.subVC[indexPath.row];
    
    [cell.contentView addSubview:VC.view];
    
    return cell;
}



#pragma  mark - 滑动视图的代理方法
//滑动结束后的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if ([scrollView isKindOfClass:[UICollectionView class]]) {
        

       //容器视图滑动到第几页
       NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
    
       [_topScrollerView scrollRectToVisible:CGRectMake(pageIndex *btnW, 0, btnW,  _topScrollerView.frame.size.height - 8) animated:YES];
       
        for (UIView *view  in _topScrollerView.subviews) {
            
            if ([view isKindOfClass:[UIButton class]]) {
                
                UIButton *btn = (UIButton *)view;

                if (btn.tag == pageIndex + 2016) {
                    
                    btn.selected = YES;
                    [self changeStatus:btn];
                }else {
                   
                    btn.selected = NO;
                    [self changeStatus:btn];
                }

            }
            
        }
        
}
    
}

#pragma  mark - 响应事件
- (void)topBtnAction:(UIButton *)btn {
    
    NSInteger  index = btn.tag - 2016;

    for (int i = 0; i < _subVC.count; i++) {
        
        UIButton *button = (UIButton *)[_topScrollerView viewWithTag:i + 2016];
        
        if (button.tag == btn.tag) {
            
            button.selected = YES;

        }else {
            
            button.selected = NO;
        }
        [self changeStatus:button];
    }
    [_viewControllers scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition: UICollectionViewScrollPositionNone animated:YES];


    
}



#pragma  mark - 选中button的事件
- (void)changeStatus:(UIButton *)btn {
    
    //选中状态改变字体大小和颜色
    if (btn.selected == YES) {
       
        btn.titleLabel.font = [UIFont systemFontOfSize:SelectFont_size];
        [btn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
    }else {
    
        //正常状态的字体大小和颜色
        btn.titleLabel.font = [UIFont systemFontOfSize:NormalFont_size];

        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    }
    
}



@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容