如何检测UIPageViewController到达最后一页

时间:2013-12-10 06:54:25

标签: ios objective-c

如何检测pageViewController中的最后一页已被调用,如果我想在最后一页添加手势识别器并转发到我的应用主页,有没有办法做到这一点?

这是我的基本pageViewController代码段, 在我的UIPageViewController viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the data model
    _pageImages = @[@"intro_1@2x.png", @"intro_2@2x.png", @"intro_3@2x.png", @"intro_4@2x.png"];

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

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

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

还实现了两个页面视图控制器数据源方法

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

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

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

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

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

    index++;

    if (index == [self.pageImages count]) {
        // adding gesture
        pageViewController.view.userInteractionEnabled = YES;
        pageViewController.view.contentMode = UIViewContentModeScaleAspectFit;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self
                                             action:@selector(startAction)];
        [singleTap setNumberOfTapsRequired:1];
        pageViewController.view.userInteractionEnabled = YES;
        [pageViewController.view addGestureRecognizer:singleTap];

        return nil;
    }

    NSLog(@"index: %u", index);

    return [self viewControllerAtIndex:index];
}

它工作正常,但我不知道在到达最后一页时添加手势识别器的位置,并指向我的主页。

编辑:我在viewControllerAfterViewController中添加了手势识别器,但它不起作用,没有响应。

2 个答案:

答案 0 :(得分:2)

您已经在

中检查了您的上一页是否已到达
if (index == [self.pageImages count]) {        
    return nil;
}

因此,如果您想要做一些特别的事情,您必须在返回之前在此块中插入代码。

答案 1 :(得分:0)

修改你的方法请尝试: -

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

    if (index == NSNotFound) {
        return;
    }
    index++;
    if (index == [self.pageImages count]) {
        // adding gesture
        pageViewController.view.userInteractionEnabled = YES;
        pageViewController.view.contentMode = UIViewContentModeScaleAspectFit;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self
                                             action:@selector(startAction)];
        [singleTap setNumberOfTapsRequired:1];
        [self.view addGestureRecognizer:singleTap];
    }
    return [self viewControllerAtIndex:index];
}