Search code examples
xcodeios6pagecontrol

UIPageViewController scroll automatically


I'm using an UIPageViewController with a PageControl to display 3 images in my PageViewController. All works perfect. But is it possible that the Pages are swiping (scrolling) to the next page after a specific time automatically?


Solution

  • Use NSTimer to scroll after particular time.I hope following code will help you

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

    after that define "move_pagination" method like

    - (void)scrollingTimer {
    
    UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
    
    UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
    
    CGFloat contentOffset = scrMain.contentOffset.x;
    // calculate next page to display
    int nextPage = (int)(contentOffset/scrMain.frame.size.width) + 1 ;
    // if page is not 10, display it
    if( nextPage!=10 )  {
        [scrMain scrollRectToVisible:CGRectMake(nextPage*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
        pgCtr.currentPage=nextPage;
    // else start sliding form 1 :)
    } else {
        [scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
        pgCtr.currentPage=0;
    }
    

    }