UIPageViewController自动滚动问题

时间:2017-06-29 09:22:08

标签: ios objective-c nstimer uipageviewcontroller

我使用这样的计时器每隔5秒就实现了UIPageViewController的自动滚动:

     _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方法我的应用程序停止响应。 如果我切换到下一页仍然在每个视图上遇到同样的问题。

1 个答案:

答案 0 :(得分:1)

执行如下所示的更改,在后台线程中执行所有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];
        });



    });
}

我希望这会对你有帮助..