IOS - UIPageViewController使用页面卷曲效果转到上一页,并在每个页面上显示页码

时间:2016-10-24 12:58:51

标签: ios objective-c xcode uipageviewcontroller

我是新手使用UIPageViewController,

我想制作以相同视图左右滑动的应用。我只使用一个uitableview来无限滑动uipageviewcontroller。

Table View

使用滚动效果可以正常工作但是当我使用页面卷曲效果时,它只允许向右滑动而不会转到上一页。

这是我的PageViewController文件代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = self;
    self.delegate = self;
    [self didMoveToParentViewController:self];
    _counter = 0;

    [self setViewControllers:@[[self.storyboard instantiateViewControllerWithIdentifier:@"memoTable"]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

    // Do any additional setup after loading the view.
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

#pragma mark PageViewController delegate method

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSLog(@"Child controller counter before  = %d", _counter);

    if ([viewController isKindOfClass:[PageController class]])
        return nil;

    if(_counter < 1) {
        return nil;
    }

    self.view.backgroundColor = [UIColor lightGrayColor];
    return [self.storyboard instantiateViewControllerWithIdentifier:@"memoTable"];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSLog(@"Child controller counter after  = %d", _counter);
    if ([viewController isKindOfClass:[PageController class]])
        return nil;
    return [self.storyboard instantiateViewControllerWithIdentifier:@"pageView"];
}

那么如何使用页面卷曲效果并左右滑动来实现此目的。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您需要的是单侧无限,您需要检查index以及它是否为0 return nil

检查PageViewController的一些方法:

// Create the data model.
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        _pageData = [[dateFormatter monthSymbols] copy];

- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard {
    // Return the data view controller for the given index.
    if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];
    dataViewController.dataObject = self.pageData[index];
    return dataViewController;
}

- (NSUInteger)indexOfViewController:(DataViewController *)viewController {
    // Return the index of the given data view controller.
    // For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
    return [self.pageData indexOfObject:viewController.dataObject];
}

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }

    index++;
    if (index == [self.pageData count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}

您还必须管理一个数组以识别每件事index | count

在上面的情况self.pageData是我管理页面的数组。 如果您需要帮助,请告诉我。