ios – 如何检测UIPageViewController到达最后一页

如何检测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中添加了手势识别器,但它不起作用,没有响应.

最佳答案 修改你的方法请尝试: –

- (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];
}
点赞