停止UIPageViewController的滚动事件

时间:2015-04-23 14:08:32

标签: ios uiscrollview scrollview uipageviewcontroller

我的ViewController里面有一个UIPageViewController。我的PageViewController中有四个视图。我希望视图不应在右侧滚动的第一个视图和左侧滚动的第四个视图上滚动。我怎么做?基本上我需要像(BOOL)scrollViewShouldScroll:

这样的方法

我尝试过使用scrollViewDidBeganDragging并设置了contentOffset属性,将panGestureRecognizer&#39}设置为false/true,但确实会停止滚动但运气不佳。

1 个答案:

答案 0 :(得分:0)

我创建了两个ViewController:

  • RootViewController:我在哪里管理UIPageViewController
  • DataViewcontroller :我在哪里展示我想要的内容(我们的页面)

    class RootViewController: UIViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
    {
      var pageData:[String] = [] // where I store images names
    
    }
    

实施这两种方法:

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

  if !viewController.isKindOfClass(DataViewController) // Specify your type
  {
    return nil
  }

    var index:Int = indexOfViewController(viewController as! DataViewController)
  if index == 0
  {
    return nil
  }
  else if index == NSNotFound
  {
    return nil
  }

  index--
  return self.viewControllerAtIndex(index)

}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

  if !viewController.isKindOfClass(DataViewController)  // Specify your type
  {
    return nil
  }

  var index:Int = indexOfViewController(viewController as! DataViewController)
  if index == NSNotFound
  {
    return nil
  }

  index++   
  if index == self.pageData.count 
  {
    return nil
  }     

  return self.viewControllerAtIndex(index)
}

个人职能:

func indexOfViewController(viewController: DataViewController) -> Int
{
  return find(pageData, viewController.dataObject)!
}

func viewControllerAtIndex( index: Int) -> DataViewController! {
  if pageData.count == 0 || index >= pageData.count {
    return nil
  }

  var dataVC:DataViewController = self.storyboard!.instantiateViewControllerWithIdentifier("DataViewController") as! DataViewController
  dataVC.dataObject = pageData[index] as String

  return dataVC
}