UIPageViewController---iOS笔记摘录

创建UIPageViewController

    // 创建UIPageViewController ,style:滚动(默认),方向:横向(默认)
    // style(2种): pageCurl翻书,scroll滚动
    // orientation(2种):横向,纵向
    // options(2种):
    // UIPageViewControllerOptionSpineLocationKey(仅在style为翻书时有效):书脊位置。
    /*
     UIPageViewControllerSpineLocationNone = 0, // 若style不是翻页
     UIPageViewControllerSpineLocationMin = 1,  // Requires one view controller.
     UIPageViewControllerSpineLocationMid = 2,  // Requires two view controllers.
     UIPageViewControllerSpineLocationMax = 3   // Requires one view controller.
     }
     */
    // UIPageViewControllerOptionInterPageSpacingKey(仅在style为scrollview时有效):2个页面之间的间距



    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil}];

    // dele,dataSource< UIPageViewControllerDelegate >< UIPageViewControllerDataSource >
    [self.pageViewController setDelegate:self]; 
    [self.pageViewController setDataSource:self]; 
    // bgColor
    [self.pageViewController.view setBackgroundColor:[UIColor whiteColor]];
    // frame
    [self.pageViewController.view setFrame:CGRectMake(0, 31, kScreenW, kScreenH-31)];
    // 滚动到 显示VC(方向:Forward下一个   Reverse上一个)
    [_pageViewController setViewControllers:@[[self viewControllerAtIndex:0]]direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];



    // readOnly
    [self.pageViewController transitionStyle];
    [self.pageViewController navigationOrientation];
    [self.pageViewController spineLocation];
    [self.pageViewController gestureRecognizers];
    [self.pageViewController viewControllers];

Delegate DataSource

#pragma mark UIPageViewControllerDelegate
// 过渡动画结束后调用(用于协同其他View变化)
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    
    // 过渡动画完成
    if (completed) {
        
        // 之前的Controller
    previousViewControllers
        
        // 当前VC
    pageViewController.viewControllers.lastObject;
        // 当前VC
    pageViewController.viewControllers
    }
}//
-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{}
//
- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController{}
//
- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController{}
//
-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{}


#pragma mark UIPageViewControllerDataSource
// 返回上一个VC
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSUInteger index = [self indexOfViewController:viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }
    self.currentIndex = index;
    index--;
    
    return [self viewControllerAtIndex:index];
}
// 返回下一个VC
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    //
    NSUInteger index = [self indexOfViewController:viewController];
    if (index == NSNotFound) {
        return nil;
    }
    self.currentIndex = index;
    index++;
    if (index == [self.pageContentArray count]) {
        return nil;
    }
    
    return [self viewControllerAtIndex:index];
}
//
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{}
//
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{}

辅助方法

// 返回坐标
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
    //
    if (([self.pageContentArray count] == 0) || (index >= [self.pageContentArray count])){
        return nil;
    }
    UIViewController *contentVC = self.pageContentArray[index];
    
    return contentVC;
}
// 返回坐标对应的VC
- (NSUInteger)indexOfViewController:(UIViewController *)viewController{
    
    //
    __block NSInteger currentIdx = 0;
    [self.pageContentArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqual:viewController]) {
            currentIdx = idx;
            *stop = YES;
        }
    }];
    return currentIdx;
}

// 其它视图做相应变化时
- (void)changeVC:(NSInteger )idx{
    
    //
    //滚动方向
    NSUInteger direction;
    if (_currentIndex>idx) {
        direction = UIPageViewControllerNavigationDirectionReverse;
    }else {
        direction = UIPageViewControllerNavigationDirectionForward;
    }
    [_pageViewController setViewControllers:@[_pageContentArray[idx]] direction:direction animated:YES completion:^(BOOL finished) {
        _currentIndex = idx;
    }];
}

数据源

数据源数组---存放VC

你可能感兴趣的:(UIPageViewController---iOS笔记摘录)