如何在 iOS 中自动滚动 UIPage 视图控制器?

How to scroll UIPage view controller Automatically in iOS?

在我的应用程序中。我想设计教程屏幕,我正在使用页面视图控制器。我希望我的页面视图控制器自动滚动并且页面控件也更新。

帮帮我。

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{

    NSUInteger index=((PageContantViewController *)viewController).pageIndex;
    if(index==NSNotFound){
        return nil;
    }
    index++;
    if(index==[self.pageTitles  count]){
        return nil;
    }
    return [self viewControllerAtIndex:index];
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{

    NSUInteger index= ((PageContantViewController *)viewController).pageIndex;
    if(index==0 || index==NSNotFound){
        return nil;
    }
    index--;
    return [self viewControllerAtIndex:index];
}

- (PageContantViewController *)viewControllerAtIndex:(NSUInteger)index{
    if([self.pageTitles count]==0 || index>=[self.pageTitles count]){
        return  nil;
    }
    PageContantViewController *contantVC=[self.storyboard instantiateViewControllerWithIdentifier:@"pageContantViewController"];
    contantVC.titleText= _pageTitles[index];
    contantVC.imageFile= _pageImages[index];
    contantVC.pageIndex= index;

    return contantVC;
}

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

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

如果在故事板中创建 PageViewController:

在左侧菜单中将过渡样式设置为滚动。

如果以编程方式创建 PageViewController:

-self.pageViewController.transitionStyle =UIPageViewControllerTransitionStyle.Scroll

请使用此代码,它会对您有所帮助

- (void)viewDidLoad {
    [super viewDidLoad];

    currentIndex = 0;
    self.delegate = self;
    self.dataSource = self
    // setup time interval here

    [NSTimer scheduledTimerWithTimeInterval:10.0
                                     target:self
                                   selector:@selector(loadNextController)
                                   userInfo:nil
                                    repeats:YES];

    // set main controller
    NSArray *mainViewControllers = @[[self  viewControllerAtIndex:self.index]];

    [self setViewControllers: startingViewControllers
                   direction: UIPageViewControllerNavigationDirectionForward
                    animated: YES
                  completion: nil];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return currentIndex;
}

- (void)loadNextController {

    PageContentViewController *nextViewController = [self viewControllerAtIndex:currentIndex++];
    if (nextViewController == nil) {

        currentIndex = 0;
        nextViewController = [self viewControllerAtIndex:currentIndex];
    }

    [self setViewControllers:@[nextViewController]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
}