码迷,mamicode.com
首页 > 其他好文 > 详细

UIPageViewController

时间:2018-12-26 15:44:44      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:key   raft   curl   nss   form   eal   gem   cin   horizon   

先说初始化

- (UIPageViewController *)PageViewController{
    if(!_PageViewController){
        //书脊位置,只有在UIPageViewControllerTransitionStylePageCurl才生效
        NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
                                                           forKey: UIPageViewControllerOptionSpineLocationKey];
        //两个page之间的间距,只有UIPageViewControllerTransitionStyleScroll格式才生效
//        NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:0]
//                                                           forKey: UIPageViewControllerOptionInterPageSpacingKey];
        _PageViewController = [[ UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
        _PageViewController.view.backgroundColor = [UIColor clearColor];
        _PageViewController.dataSource = self;
        _PageViewController.delegate = self;
    }
    return _PageViewController;
}

然后填充内容

//填充PageView
- (void)setPageViewData{
    [self.controllerArray removeAllObjects];
    ReadingSubViewController *subView = [self viewControllerAtIndex:0];
    [self.controllerArray addObject:subView];
    [self.PageViewController setViewControllers:self.controllerArray direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
    }];
}

- (ReadingSubViewController *)viewControllerAtIndex:(NSInteger)index{
    NSLog(@"push%ld",index);
    ReadingSubViewController *subView = [[ReadingSubViewController alloc]init];
    subView.pageModel = self.bookpageData[index];
    subView.bookModel = self.model;
    subView.pageIndex = index;
    _open = YES;
    [self pushButtonClick];
    return subView;
}

 &&重点

 重点是是他内部的运行逻辑,我也是今天才搞清楚。

先看delegate

#pragma mark ==========PageVCDelegate==========
//这个方法是返回前一个页面,如果返回为nil,那么UIPageViewController就会认为当前页面是第一个页面不可以向前滚动或翻页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSInteger index = [self getIndex:(ReadingSubViewController *)viewController];
    NSLog(@"before=%ld",index);
    if (index == 0 || index == NSNotFound) {
        return nil;
    }
    index -- ;
    ReadingSubViewController *playVC = [self viewControllerAtIndex:index];
    if (index+1< 10) {
        self.pageNumberLB.text = [NSString stringWithFormat:@"0%ld/%ld",index+1,_maxPage];
    }else{
        self.pageNumberLB.text = [NSString stringWithFormat:@"%ld/%ld",index+1,_maxPage];
    }
    return playVC;
}
//这个方法是下一个页面,如果返回为nil,那么UIPageViewController就会认为当前页面是最后一个页面不可以向后滚动或翻页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSInteger index = [self getIndex:(ReadingSubViewController *)viewController];
    if (index == NSNotFound || index == _maxPage - 1) {
        return nil;
    }
    NSLog(@"after=%ld",index);
    index ++;
    ReadingSubViewController *playVC = [self viewControllerAtIndex:index];
    if (index+1< 10) {
        self.pageNumberLB.text = [NSString stringWithFormat:@"0%ld/%ld",index+1,_maxPage];
    }else{
        self.pageNumberLB.text = [NSString stringWithFormat:@"%ld/%ld",index+1,_maxPage];
    }
    return playVC;
}
- (NSInteger)getIndex:(ReadingSubViewController *)subVC{
    return subVC.pageIndex;
}

 

如果TransitionStyle是UIPageViewControllerTransitionStylePageCurl

那么从首页开始拖动的时候,往左拖动,会触发viewControllerBeforeViewController这个代理方法,并不会触发viewControllerAfterViewController代理方法。

然后每次往右拖动时只会加载一个ViewController。上面整个流程的Log如下

 

eadingViewController.m[88] push0
ReadingViewController.m[102] before=0
ReadingViewController.m[121] after=0
ReadingViewController.m[88] push1
ReadingViewController.m[136] 开始滚动
ReadingViewController.m[140] isCompleted:成功
ReadingViewController.m[121] after=1
ReadingViewController.m[88] push2
ReadingViewController.m[136] 开始滚动
ReadingViewController.m[140] isCompleted:成功

 

如果TransitionStyle是UIPageViewControllerTransitionStyleScroll

那么从首页开始拖动,不管是往右还是往左,都肯定会viewControllerAfterViewController,为PageController加载好下个Page的ViewController。我往左拖动的Log如下

ReadingViewController.m[88] push0
ReadingViewController.m[102] before=0
ReadingViewController.m[121] after=0
ReadingViewController.m[88] push1

虽然拖动完成的代理方法没有执行。但是 viewControllerAfterViewController方法触发,加载好了下一页的ViewController内容。

然后往右拖动会提前加载第三页的内容Log如下

ReadingViewController.m[102] before=0
ReadingViewController.m[121] after=0
ReadingViewController.m[88] push1
ReadingViewController.m[136] 开始滚动
ReadingViewController.m[140] isCompleted:成功
ReadingViewController.m[121] after=1
ReadingViewController.m[88] push2

如果是开始就往右拖动的话Log如下

ReadingViewController.m[88] push0
ReadingViewController.m[121] after=0
ReadingViewController.m[88] push1
ReadingViewController.m[102] before=0
ReadingViewController.m[136] 开始滚动
ReadingViewController.m[140] isCompleted:成功
ReadingViewController.m[121] after=1
ReadingViewController.m[88] push2
ReadingViewController.m[136] 开始滚动
ReadingViewController.m[140] isCompleted:成功
ReadingViewController.m[121] after=2
ReadingViewController.m[88] push3

 这是拖动到第三页的时候,但是第四页其实已经预加载好了。

之前一直没有用过UIPageViewControllerTransitionStyleScroll,今天用了一次才发现自己认识的很不清晰。做下记录

 

 

UIPageViewController

标签:key   raft   curl   nss   form   eal   gem   cin   horizon   

原文地址:https://www.cnblogs.com/wycstudy/p/10179036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!