将计时器设置为UIPageViewController

时间:2015-03-31 00:38:44

标签: ios swift nstimer 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
}

1 个答案:

答案 0 :(得分:4)

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

我花了更多的时间,而不是试图想出这个场景,目前有以下工作。我原以为这是苹果提供的基本功能。

  • 首先,我在为内容创建的每个ViewController上设置了一个itemIndex属性。
  • 其次,查询childViewController以获取索引。
  • 然后转换到索引+ 1。

功能:

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
}

记录......每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)
}