1

I'm trying to have the UIPageViewController navigate through the pages for every 5 seconds. How to do this in Swift?

Is there any way to explicitly call viewControllerAfterViewController?

 var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("update"), userInfo: nil, repeats: true)

  func update() -> UIViewController  {
              // can u please tell me what to add here 
    }



func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{

    var index = (viewController as tourViewControllerhelper).pageIndex

    if (index == 0) || (index == NSNotFound) {
         return viewControllerAtIndex(4)
    }

    index--
    pageCounter--
    return viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{

    var index = (viewController as tourViewControllerhelper).pageIndex

    if (index == NSNotFound || index == 4) {
        return viewControllerAtIndex(0)
    }

    index++
    pageCounter++

    return viewControllerAtIndex(index)
}
func viewControllerAtIndex(index: Int) -> tourViewControllerhelper?
{
    if self.pageTitles == 0 || index >= self.pageTitles
    {
        return nil
    }


    // Create a new view controller and pass suitable data.
    let pageContentViewController = tourViewControllerhelper()

    pageContentViewController.titleText = tourTextDescription[index]
    pageContentViewController.pageIndex = index
    currentIndex = index

    println("xyz")
    return pageContentViewController
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Sruthin Gaddam
  • 159
  • 2
  • 14
  • Almost a duplicate, but not sure if it counts as a duplicate in swift: http://stackoverflow.com/questions/7208871/is-it-possible-to-turn-page-programmatically-in-uipageviewcontroller – danh Mar 31 '15 at 00:41
  • But that is in obj -c I need it in swift.. I can get the pages to move but not the little dots on the bottom – Sruthin Gaddam Mar 31 '15 at 02:36
  • That's why I didn't vote to close. In your update method, advance the page number and call setViewControllers:direction:animated:completion:. If it works, question answered, if not, that's the code that needs to be reviewed on SO. Paste it here. – danh Mar 31 '15 at 02:40
  • func update() { if counter >= 5{ counter = 0 } let startingViewController: tourViewControllerhelper = viewControllerAtIndex(counter)! let viewControllers: NSArray = [startingViewController] self.pageViewController?.setViewControllers(viewControllers, direction: .Forward , animated: true, completion: nil) self.pageControl.currentPage = counter println(self.pageControl.currentPage) counter++ } – Sruthin Gaddam Mar 31 '15 at 02:54
  • This is the code .. The page slides but the little dots on the bottom doesn't slide – Sruthin Gaddam Mar 31 '15 at 02:54
  • self.pageControl.currentPage = counter – Sruthin Gaddam Mar 31 '15 at 03:00
  • self.pageControl.currentPage = counter this is the part that doesn't work – Sruthin Gaddam Mar 31 '15 at 03:01

1 Answers1

4

I think the problem with the code keeping a counter is that it doesn't account for the pageViewController being manually scrolled as well or the counter goes out of sync.

I've spent more time than I would have thought trying to figure this scenario out and currently have the following working. I would have thought this was a basic function apple would provide.

  • First I have an itemIndex property set on each ViewController created for the content.
  • Second, query the childViewController for the index.
  • Then do a transition to the index+1.

function:

func advancePage () {
    let pvcs = pageViewController?.childViewControllers as! [PageContentViewController]
    let itemIndex = pvcs[0].itemIndex
    let firstController = getItemController(itemIndex+1)!
    let startingViewControllers = [firstController]
    pageViewController!.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
}

The getItemController() is taken from the normal setup and looks like:

private func getItemController(itemIndex: Int) -> PageContentViewController? {

    if itemIndex < contentImages.count {
        let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
        pageItemController.itemIndex = itemIndex
        pageItemController.imageName = contentImages[itemIndex]
        return pageItemController
    }

    return nil
}

For the record... the timer to happen every 5 seconds is:

private var timer: NSTimer = NSTimer()
override func viewDidLoad() {
    super.viewDidLoad()
    createPageViewController()
    timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("advancePage"), userInfo: nil, repeats: true)
}
TPot
  • 340
  • 3
  • 10
  • Excellent answer. The only issue is that I have a video playing on each screen and the transition gets a bit "funky", but this code works very well. – damianesteban Jan 30 '16 at 21:26