實(shí)現(xiàn)如下效果:

pageView
分析
通過傳入標(biāo)題數(shù)組, 視圖控制器數(shù)組初始化 UIScrollView, 實(shí)現(xiàn)翻頁.
通過 ViewController的 addChildrenViewCrotroller 來讓各個自視圖控制器控制子視圖.
實(shí)現(xiàn)
代理方法
類似系統(tǒng) UITableView 的實(shí)現(xiàn)方式, 定義數(shù)據(jù)源,與代理方法
- id<LUHPageViewControllerSourceDate> sourceData
@protocol LUHPageViewControllerSourceDate <NSObject>
@required
-(NSInteger)widthWithSingleTitle;
-(NSArray *)titlesForPage:(LUHPageViewController *)pageView;
-(NSArray *)contentViewControllersForPage:(LUHPageViewController *)pageView;
@end
- id<LUHPageViewControllerDelegate> delegate
@protocol LUHPageViewControllerDelegate <NSObject>
-(void)pageDidShow:(UIViewController *)viewController;
@end
主要實(shí)現(xiàn)邏輯
設(shè)置標(biāo)題
-(void)addTitle {
NSArray *titles = [self.sourceData titlesForPage:self];
NSInteger titlesWidth = [self.sourceData widthWithSingleTitle];
if (self.titleStyle == LUHPageViewTitleStyleSegment) {
_segmentControl = [[UISegmentedControl alloc] initWithItems:titles];
_segmentControl.selectedSegmentIndex = 0;
_segmentControl.frame = CGRectMake(0, 0, titles.count * titlesWidth, kLUHPageViewControllerTitleHeight);
[_segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.navigationItem setTitleView:_segmentControl];
} else if (self.titleStyle == LUHPageViewTitleStyleUnderlineButton) {
_underLineButtonView = [[LUHUnderLineButtonView alloc] initWithItems:titles];
_underLineButtonView.frame = CGRectMake(0, 0, titles.count * titlesWidth, kLUHPageViewControllerTitleHeight);
[_underLineButtonView addTarget:self action:@selector(underLineButtonAction:)];
[self.navigationItem setTitleView:_underLineButtonView];
}
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:.7 green:.7 blue:.8 alpha:1];
}
設(shè)置 scrollView
-(void)addContentViewController
{
NSArray *contents = [self.sourceData contentViewControllersForPage:self];
_contentsView = [NSArray arrayWithArray:contents];
int i = 0;
for (UIViewController *page in contents) {
[self addChildViewController:page];
page.view.frame = CGRectMake(CGRectGetWidth(self.view.frame)*i,
0, CGRectGetWidth(self.view.frame),
CGRectGetHeight(self.view.frame));
[self.scrollView addSubview:page.view];
i++;
}
}
附 源代碼