使用下面的代碼:
//array with all pages
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [
Utils.newViewController(name: "PhotoOfTheDayViewController"),
Utils.newViewController(name: "PhotosViewController"),
Utils.newViewController(name: "PhotosViewController"),
Utils.newViewController(name: "PhotosViewController"),
Utils.newViewController(name: "InfoViewController")
]
}();
private var currentIndex : Int!;
//jump to first page
@objc private func home() {
self.jumptoPage(ind: 0);
}
private func jumptoPage(ind : Int) {
var index = ind;
let vc = self.orderedViewControllers.first;
let direction : UIPageViewControllerNavigationDirection!;
if currentIndex < index {
direction = .forward;
index += 1;
} else {
direction = .reverse;
index -= 1;
}
if (currentIndex < index) {
for i in 0 ..< index + 1 {
if (i == index) {
self.setViewControllers([vc!], direction: direction, animated: true, completion: nil);
} else {
self.setViewControllers([self.orderedViewControllers[i]], direction: direction, animated: false, completion: nil);
}
}
} else {
for i in stride(from: currentIndex, to: index - 1, by: -1) {
if i == index {
self.setViewControllers([vc!], direction: direction, animated: true, completion: nil);
} else {
self.setViewControllers([self.orderedViewControllers[i]], direction: direction, animated: false, completion: nil);
}
}
}
currentIndex = ind;
}
使用'protocol'模式。你的按鈕在哪裏?它在ViewController中滑動嗎? –
檢查https://github.com/mattneub/pageViewControllerUsingInternalButtons –
是的。該按鈕位於ViewController中,因爲我在PageViewController中有3張幻燈片。該按鈕將從PageViewController類調用changeVC方法。順便說一句,是不可能使用setViewControllers? –