//
// ViewController.m
// scrollview加collectionview
//
// Created by 洪福清 on 16/11/29.
// Copyright © 2016年 BJTYL. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
#import "ThridViewController.h"
#import "UIColor+SPAddition.h"
#define SP_NavitionBarBackColor @"#1894d1"
@interface ViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) UISegmentedControl *segmentControl;
@property (strong, nonatomic) UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 初始化子控制器
[self setupAllChildVcs];
[self addController];
// 添加第0个子控制器的view
[self addChildVcViewIntoScrollView:0];
}
- (void)setupAllChildVcs
{
[self addChildViewController:[[SecondViewController alloc] init]];
[self addChildViewController:[[ThridViewController alloc] init]];
}
- (void)addController
{
[self.view addSubview:self.segmentControl];
[self.view addSubview:self.scrollView];
}
-(UISegmentedControl *)segmentControl
{
if (_segmentControl == nil) {
NSArray *array = [NSArray arrayWithObjects:@"候车信息",@"到达信息",nil];
_segmentControl = [[UISegmentedControl alloc] initWithItems:array];
_segmentControl.selectedSegmentIndex = 0;
_segmentControl.frame = CGRectMake(50, 70, self.view.frame.size.width-100, 40);
_segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
_segmentControl.tintColor = [UIColor colorWithHexString:SP_NavitionBarBackColor];
[_segmentControl addTarget:self action:@selector(segemntControlClick:) forControlEvents:UIControlEventValueChanged];
}
return _segmentControl;
}
-(UIScrollView *)scrollView
{
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-110)];
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width*2, 0);
}
return _scrollView;
}
- (void)segemntControlClick:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
case 0:
{
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = CGPointMake(0, 0);
} completion:^(BOOL finished) {
// 添加子控制器的view
[self addChildVcViewIntoScrollView:0];
}];
}
break;
case 1:
{
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
} completion:^(BOOL finished) {
// 添加子控制器的view
[self addChildVcViewIntoScrollView:1];
}];
}
default:
break;
}
}
- (void)addChildVcViewIntoScrollView:(NSUInteger)index
{
UIViewController *childVc = self.childViewControllers[index];
// 如果view已经被加载过,就直接返回
if (childVc.isViewLoaded) return;
// 取出index位置对应的子控制器view
UIView *childVcView = childVc.view;
// 设置子控制器view的frame
CGFloat scrollViewW = self.scrollView.frame.size.width;
childVcView.frame = CGRectMake(index * scrollViewW, 0, scrollViewW, self.scrollView.frame.size.height);
// 添加子控制器的view到scrollView中
[self.scrollView addSubview:childVcView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 1.取出水平方向上滚动的距离
CGFloat offsetX = scrollView.contentOffset.x;
// 2.求出页码
double pageDouble = offsetX / scrollView.frame.size.width;
int pageInt = (int)(pageDouble + 0.5);
self.segmentControl.selectedSegmentIndex = pageInt;
}
@end