@milijan 回答效果很好。这是他回答的快速版本:
func jumptoPage(index : Int) {
let vc = viewControllerAtIndex(index)
let direction : UIPageViewControllerNavigationDirection!
if currentIndex < index {
direction = UIPageViewControllerNavigationDirection.Forward
}
else {
direction = UIPageViewControllerNavigationDirection.Reverse
}
if (currentIndex < index) {
for var i = 0; i <= index; i++ {
if (i == index) {
self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil)
}
else {
self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil)
}
}
}
else {
for var i = currentIndex; i >= index; i = i - 1 {
if i == index {
self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil)
}
else {
self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil)
}
}
}
currentIndex = index
}