Set timer to UIPageViewController

一世执手 提交于 2019-12-03 21:36:31

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)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!