使用以下代码,页面控件在单个页面交换期间跳转两次而不是一次。我需要将UIPageViewController
添加为子视图,如下所示,如何使页面控件正常运行?谢谢!
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate{
var pageViewController: UIPageViewController!
let pagesArray = ["Page1ViewController", "Page2ViewController","Page3ViewController"]
let pageControl : UIPageControl = UIPageControl(frame: CGRectMake(100, 600, 200, 20))
func viewControllerAtIndex(index: Int) -> UIViewController? {
let vc = storyboard?.instantiateViewControllerWithIdentifier (pagesArray[index])
return vc
}
func pageViewController(pageViewController:UIPageViewController,
viewControllerBeforeViewController
viewController: UIViewController) -> UIViewController?{
if var index = pagesArray.indexOf(viewController.restorationIdentifier!){
if index > 0 {
index--
self.pageControl.currentPage = index
return viewControllerAtIndex(index)
}
}
return nil
}
func pageViewController(pageViewController:UIPageViewController,
viewControllerAfterViewController
viewController: UIViewController) -> UIViewController?{
if var index = pagesArray.indexOf(viewController.restorationIdentifier!){
if index < pagesArray.count - 1 {
index++
return viewControllerAtIndex(index)
}
}
return nil
}
override func viewDidLoad() {
super.viewDidLoad()
pageControl.numberOfPages = 3
pageControl.currentPage = 0
pageControl.tintColor = UIColor.redColor()
pageControl.pageIndicatorTintColor = UIColor.blackColor()
pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
if let vc = storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController")
{
self.addChildViewController(vc)
self.view.addSubview(vc.view)
self.view.addSubview(pageControl)
pageViewController = vc as! UIPageViewController
pageViewController.dataSource = self
pageViewController.delegate = self
pageViewController.setViewControllers([viewControllerAtIndex(0)!], direction: .Forward, animated: true, completion: nil)
pageViewController.didMoveToParentViewController(self)
}
}
}
试试这个
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
let identifier = viewController.restorationIdentifier
self.index = self.identifiers.indexOfObject(identifier!)
if index == identifiers.count - 1 {
return nil
}
//increment the index to get the viewController before the current one
self.index = self.index + 1
return self.viewControllerAtIndex(self.index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let identifier = viewController.restorationIdentifier
self.index = self.identifiers.indexOfObject(identifier!)
if index == 0 {
return nil
}
//decrement the index to get the viewController before the current one
self.index = self.index - 1
return self.viewControllerAtIndex(self.index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return self.identifiers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}