iOS UIPageViewController

2016.11.7

创建一个UIPageViewController并初始化

NSDictionary*options = [NSDictionarydictionaryWithObject:[NSNumbernumberWithInteger:UIPageViewControllerSpineLocationMin]forKey:UIPageViewControllerOptionSpineLocationKey];

[[UIPageViewControlleralloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScrollnavigationOrientation:UIPageViewControllerNavigationOrientationHorizontaloptions:options];

设置delegate、dataSource

设置page的位置

pageController.view.frame = ......;

设置page的第一个子控制器

[_pageControllersetViewControllers:[NSArrayarrayWithObject:_pageArr[0]]direction:UIPageViewControllerNavigationDirectionForwardanimated:NOcompletion:nil];

[selfaddChildViewController:_pageController];

[self.viewaddSubview:_pageController.view];

[_pageControllerdidMoveToParentViewController:self];

初始化盛放子控制器的数组和子控制器

_person= [[PersonalViewControlleralloc]init];

_school= [[SchoolViewControlleralloc]init];

_relation= [[RelationViewControlleralloc]init];

_ohter= [[OtherViewControlleralloc]init];

_pageArr= [[NSMutableArrayalloc]init];

[_pageArraddObject:_person];

[_pageArraddObject:_school];

[_pageArraddObject:_relation];

[_pageArraddObject:_ohter];

实现代理方法

#pragma mark -- page相关

//返回前一个页面,如果返回为nil,那么UIPageViewController就会认为当前页面是第一个页面不可以向前滚动或翻页

-(UIViewController*)pageViewController:(UIPageViewController*)pageViewController viewControllerBeforeViewController:(UIViewController*)viewController {

NSIntegerindex = [_pageArrindexOfObject:viewController];

index --;

if((index <0) || (index ==NSNotFound)) {

returnnil;

}

return_pageArr[index];

}

//返回下一个页面,如果返回为nil,那么UIPageViewController就会认为当前页面是最后一个页面不可以向后滚动或翻页

- (UIViewController*)pageViewController:(UIPageViewController*)pageViewController viewControllerAfterViewController:(UIViewController*)viewController {

NSIntegerindex = [_pageArrindexOfObject:viewController];

index ++;

if(index >=_pageArr.count) {

returnnil;

}

return_pageArr[index];

}

//开始滚动或翻页的时候触发

- (void)pageViewController:(UIPageViewController*)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {

}

//结束滚动或翻页的时候触发

- (void)pageViewController:(UIPageViewController*)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {

}

//配合viewController的代理方法来进行页面切换

- (void)clickButtonWithCurrentSelectedIndex:(NSInteger)currentSelectedIndex direction:(UIPageViewControllerNavigationDirection)direction {

if(currentSelectedIndex <_buttonArr.count) {

[_pageControllersetViewControllers:@[_pageArr[currentSelectedIndex]]direction:directionanimated:YEScompletion:nil];

}

}

你可能感兴趣的:(iOS UIPageViewController)