Auto scroll scrolling UIPageViewController

I did an auto-scroll UIPageViewController

every 5 seconds using a timer:

     _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];


    }

      

and deleting the timer in viewWillDisappear

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

    [_bannerTimer invalidate];
}

      

Problem:

My problem is that the method loadNextController

is called app shutdown. If I switch to the next page, I still get the same problem in all views.

+3


source to share


1 answer


Make some changes like below, do all tasks non-ui

in background thread and everything UI

in main thread, try

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



    });
}

      



Hope this helps you.

+1


source







All Articles