Get Page View to pop off on last screen

2019-09-04 06:31发布

I have a functional tutorial intro implemented into my app, but i just can't seem to get the page view to pop off when i swipe the screen for the last time. I thought dismissViewControllerAnimated:completion: would work

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

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

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

I figured out how to get it to go away, but it goes away right when it gets to the last page. I want it to show the last page and then go away when the user swipes right again on that last page.

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

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

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

2条回答
ゆ 、 Hurt°
2楼-- · 2019-09-04 06:54

Page View Controller is your main viewcontrollers child view controller, this is the correct way to remove childViewController:

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

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

    index++;

    if (index == [self.pageTitles count]) {
        [pageViewController willMoveToParentViewController:nil];  // 1
        [pageViewController.view removeFromSuperview];            // 2
        [pageViewController removeFromParentViewController];      // 3   
    }

    return [self viewControllerAtIndex:index];
}
查看更多
叛逆
3楼-- · 2019-09-04 07:05

Also encountered the same issue and my solution was to add an extra page that was clear with the following code:

/// the index of the currently displayed page
fileprivate var currentIndex: Int = 0


func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

    // didFinishAnimating gets called before viewControllerAfter, which means `currentIndex` will be 1 page behind, hence subtract 2 instead of 1
    if currentIndex == myViewControllers.count - 2 {
        //self.dismiss(animated: true, completion: nil)
        //.... dismiss pages here
    }
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    guard let viewControllerIndex = myViewControllers.index(of: viewController) else { return nil }
    let previousIndex = viewControllerIndex - 1

    // track current page index
    currentIndex = Int(viewControllerIndex)

    // ..
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    guard let viewControllerIndex = myViewControllers.index(of: viewController) else { return nil }

    let nextIndex = viewControllerIndex + 1

    // track page index
    currentIndex = Int(viewControllerIndex)

    // get next page
    if nextIndex < myViewControllers.count {
        return myViewControllers[nextIndex]
    }

    return nil
}

didFinishAnimating is called before viewControllerAfter. This means the value for currentIndex will be for the previous page when didFinishAnimating is called. Hence why I subtract 2 (instead of 1)

查看更多
登录 后发表回答