UIPageViewController自动滚动问题

UIPageViewController scroll automatically issue

我已经实现了 UIPageViewController 每 5 秒自动滚动一次,使用这样的计时器:

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


  - (void)loadNextController
        {

        self.index++;

        HeaderAdVC *nextViewController;
        if(self.index>arrayImages.count-1)
        {
            self.index=0;
            [pageIndicator setCurrentPage:0];
            nextViewController = [self viewControllerAtIndex:0];
        }
        else
        {
            nextViewController = [self viewControllerAtIndex:self.index];
        }


        [pageIndicator setCurrentPage:self.index];
        [pageController setViewControllers:@[nextViewController]
                                 direction:UIPageViewControllerNavigationDirectionForward
                                  animated:YES
                                completion:nil];


    }

并删除 viewWillDisappear 中的计时器

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [_bannerTimer invalidate];
}

问题:

我的问题是当调用 loadNextController 方法时我的应用程序停止响应。 如果我切换到下一页,在每个视图中仍然遇到相同的问题。

像下面做一些更改,在后台线程中执行所有 non-ui 任务,在主线程中执行所有 UI 操作,试试这个

- (void)loadNextController
{

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

        self.index++;

        HeaderAdVC *nextViewController;
        if(self.index>arrayImages.count-1)
        {
            self.index=0;
            [pageIndicator setCurrentPage:0];
            nextViewController = [self viewControllerAtIndex:0];
        }
        else
        {
            nextViewController = [self viewControllerAtIndex:self.index];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [pageIndicator setCurrentPage:self.index];
            [pageController setViewControllers:@[nextViewController]
                                     direction:UIPageViewControllerNavigationDirectionForward
                                      animated:YES
                                    completion:nil];
        });



    });
}

希望对您有所帮助..