//添加子視圖控制器
- (void) setupChildViewControllers
{
NSArray * vcNames = @[@"PersonageFinancialViewController",@"EnterpriseFinancialViewController"];
for (NSInteger i = 0; i < vcNames.count; i ++) {
NSString * vcName = vcNames[i];
UIViewController * vc = [[NSClassFromString(vcName) alloc] init];
//當(dāng)執(zhí)行這句話,不會(huì)執(zhí)行vc的viewDidLoad
[self addChildViewController:vc];
}
//進(jìn)入子控制器加載第一個(gè)頁(yè)面
[self scrollViewDidEndDecelerating:self.scrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat width = SCREEN_WIDTH;
CGFloat heitht = SCREEN_HEIGHT;
CGFloat offset = scrollView.contentOffset.x;
//獲取索引值
NSInteger idx = scrollView.contentOffset.x / width;
//根據(jù)索引值返回vc引用
UIViewController * vc = self.childViewControllers[idx];
//判斷當(dāng)前vc是否執(zhí)行過(guò)viewDidLoad
if ([vc isViewLoaded]) return;
//設(shè)置子控制器view的大小
vc.view.frame = CGRectMake(offset, 0, width, heitht);
//將子控制器的view加入scrollview上
[scrollView addSubview:vc.view];
}