我目前正在尝试制作的应用程序处于停滞状态。
我创建了一个页面视图控制器,它可以在一个视图控制器中滚动并显示数组中的四个不同图像和标签。这是代码实现:
_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 页。
我试图把它变成一个简单的术语来理解,但我根本无法理解我是如何做到这一点的。我花了一整天的时间试图弄清楚这一点,但我没有运气!
谁能指出我正确的方向?