-1
我是從Duc Tran關於UIPageViewController的這個很棒的教程。我想知道如何讓你的UIPageViewController內部的控制器自動轉換而不需要用戶刷卡。這就是沒有委託和數據源的情況下代碼的外觀。如何自動製作UIPageViewController開關控制器
class AnimesPageVC: UIPageViewController {
override var navigationOrientation: UIPageViewControllerNavigationOrientation {return .horizontal}
weak var pageViewControllerDelegate: AnimePagesVCDelegate?
var timeInterval: Int?
var images: [UIImage]?
lazy var animes: [UIViewController] = {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var animes = [UIViewController]()
if let images = self.images {
for image in images {
let anime = storyboard.instantiateViewController(withIdentifier: "AnimeImage")
animes.append(anime)
}
}
self.pageViewControllerDelegate?.setUpPageController(numberOfPages: animes.count)
return animes
}()
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
dataSource = self
loadPage(atIndex: 0)
}
func loadPage(atIndex: Int) {
let anime = animes[atIndex]
var direction = UIPageViewControllerNavigationDirection.forward
if let currentAnime = viewControllers?.first {
if let currentIndex = animes.index(of: currentAnime) {
if currentIndex > atIndex {
direction = .reverse
}
}
}
configurePages(viewController: anime)
setViewControllers([anime], direction: direction, animated: true, completion: nil)
}
func configurePages(viewController: UIViewController) {
for (index, animeVC) in animes.enumerated() {
if viewController === animeVC {
if let anime = viewController as? AnimeVC {
anime.image = self.images?[index]
self.pageViewControllerDelegate?.turnPageController(to: index)
}
}
}
}
}
那麼,我將如何能夠得到這種行爲。將不勝感激任何幫助。 :)
謝謝哈立德,它w掃。但是,它只是第一次。它從第一個控制器切換到第二個控制器,但不切換到下一個控制器。它也停留在第二個控制器上,所以如果我嘗試滑動到下一個或前一個控制器,它會返回到第二個控制器。 –
,因爲你必須更新你的索引,現在你的索引是0,你加1。你必須保持你的索引也更新,所以它應該工作。我正在編輯答案,所以你可以看到如何 –