将计时器设置为UIPageViewController

我试图让UIPageViewController每5秒浏览页面。 如何在Swift中做到这一点?

有没有什么办法显式调用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 } 

我认为保持计数器的代码的问题在于,它并没有考虑手动滚动pageViewController,或者计数器不同步。

我花了更多的时间比我想象的这个情况下,目前有以下工作。 我原以为这是苹果提供的一个基本function。

  • 首先,我为每个为内容创build的ViewController设置了一个itemIndex属性。
  • 其次,查询childViewController的索引。
  • 然后转到索引+ 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) } 

getItemController()取自正常的设置,如下所示:

 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 } 

为了logging…定时器每5秒发生一次是:

 private var timer: NSTimer = NSTimer() override func viewDidLoad() { super.viewDidLoad() createPageViewController() timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("advancePage"), userInfo: nil, repeats: true) }