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
}
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.
function:
The getItemController() is taken from the normal setup and looks like:
For the record... the timer to happen every 5 seconds is: