0

我目前正在尝试制作的应用程序处于停滞状态。

我创建了一个页面视图控制器,它可以在一个视图控制器中滚动并显示数组中的四个不同图像和标签。这是代码实现:

_pageTitles = @[@"One", @"Two", @"Three", @"Four"];
_pageImages = @[@"1.png", @"2.png", @"3.png",@"4.png"];

// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;

PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);

[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

if ((index == 0) || (index == NSNotFound)) {
    return nil;
}

index--;
return [self viewControllerAtIndex:index];
}

 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

if (index == NSNotFound) {
    return nil;
}

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

- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
    return nil;
}

// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard   instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.imageFile = self.pageImages[index];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;

return pageContentViewController;


}

- (NSInteger)presentationCountForPageViewController:   (UIPageViewController*)pageViewController
 {
   return [self.pageTitles count];
}

 - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
 {
  return 0;
 }

我希望每次应用程序启动时只显示三个页面,而不是所有四个页面,而不必从数组中完全删除它,但这就是问题所在。我想设置一个条件,它首先显示中间页面,并且只显示选择页面之前和之后的页面,最后一页被隐藏。

例如

如果选择第 3 页,则可以在页面视图中看到第 2、3、4 页 如果选择第 2 页,则可以在页面视图中看到第 1、2、3 页。

我试图把它变成一个简单的术语来理解,但我根本无法理解我是如何做到这一点的。我花了一整天的时间试图弄清楚这一点,但我没有运气!

谁能指出我正确的方向?

4

1 回答 1

0

您将不得不创建一个类似的类变量,currentlySelectedPageNumber然后将所有 pageViewController 委托都基于该值。如果当前选择的是 2,那么您不需要更改任何内容,除了不允许索引大于 2(视图编号 3)。如果当前选择的是 3,那么你只需要让你的委托函数返回数字 2 作为 1,数字 3 作为 2,数字 4 作为 3。

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    NSInteger difference = index - currentlySelectedPageNumber;
    if ((index == 0) || (index == NSNotFound) && difference < 0) {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index];
}

 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    NSInteger difference = index - currentlySelectedPageNumber;
    if (index == NSNotFound || difference > 0) {
        return nil;
    }

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

- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    //For this one you need to modify index based on your currently selected value
    index = currentlySelectedPageNumber - 1 + index;
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count]) || index < 0) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    PageContentViewController *pageContentViewController = [self.storyboard   instantiateViewControllerWithIdentifier:@"PageContentViewController"];
    pageContentViewController.imageFile = self.pageImages[index];
    pageContentViewController.titleText = self.pageTitles[index];
    pageContentViewController.pageIndex = index;

    return pageContentViewController;


}
于 2014-02-21T14:48:12.767 回答