Set timer to UIPageViewController

2019-04-02 14:39发布

问题:

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
}

回答1:

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)
}