//
// 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
父子控制器的使用
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 首先我们先说明什么是父子控制器,下面我举例一段代码来说明: 而把一个控制器的view添加到另一个控...
- * 用NavigationController来push出它的子控制器我们随处可见,就这个过程的一些细节和父子控制...