Arpit Javerya
Arpit Javerya

Reputation: 473

UIPageViewController scroll automatically issue

I have implemented automatic scrolling of UIPageViewController after every 5 seconds using timer like this:

     _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 removing the timer in viewWillDisappear

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

    [_bannerTimer invalidate];
}

Problem :

My problem is when the loadNextController method is called my app stop responding. If i switch to next page still getting the same problem on every view.

Upvotes: 3

Views: 207

Answers (1)

Dhiru
Dhiru

Reputation: 3060

Do some changes like below , do all non-ui task in background thread and all UI operation in Main thread ,try this

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



    });
}

i hope this will help you ..

Upvotes: 1

Related Questions