如何在IOS Swift中捕获页面滑动事件

时间:2015-04-24 05:28:12

标签: ios swift uipageviewcontroller

我使用UIPageViewController IOS Swift开发了滑动页面。当最后一页出现时,我需要做一些事情。如何捕捉页面滑动事件。

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{

  var pageViewController : UIPageViewController!
  var pageCount = 3

  func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    //Return after viewcontroller
  }

  func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    //Return before viewcontroller
  }


  func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return pageCount
  }


  func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
  }

}

2 个答案:

答案 0 :(得分:0)

在你定义的方法中,可以像这样参与其中,

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let index : NSInteger = arrproductdetial.indexOfObject(viewController) as NSInteger
    if (index == arrproductdetial.count-1) {
        // Do whatever you are trying to do for last page
    }
    else if (index == arrproductdetial.count) {
        return nil; //the page after last page
    }
}

答案 1 :(得分:0)

您只是在

中返回pageCount

func presentationCountForPageViewController(pageViewController:UIPageViewController) - > Int

而是返回pageCount.count,如下所示:

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int 
{
    return pageCount.count
}

通过这个你可以得到总数和&你也可以得到最后一个索引。我猜这清除了你的怀疑。快乐的编码!!!