一、了解一下
@1、UIPageViewController 可以轻松在其他管理的各View间滑动切换
不仅局限于创建引导页
@2、 与导航控制器相似,翻页控制器 是一个容器控制器
@3、容器中的每一页其实都是自治的,由每一页的控制器管理
二、如何使用
@1、此处举例,引导页字页面都是复用,所以使用动态,每次切换下一页时,都会咨询数据源,所以需要遵守如下协议,实现如下方法:
UIPageViewControllerDataSource
//MARK: - UIPageViewControllerDataSource
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = (viewController as! ContentViewController).index
index -= 1
return vc(atIndex: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = (viewController as! ContentViewController).index
index += 1
return vc(atIndex: index)
}
func vc(atIndex: Int) -> ContentViewController? {
//判断index是否在合理的区间内,用Swift的if case语句
if case 0..<headings.count = atIndex {
if let contentVC = storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as? ContentViewController{
contentVC.heading = headings[atIndex]
contentVC.footer = footers[atIndex]
contentVC.imageName = images[atIndex]
contentVC.index = atIndex
return contentVC
}
}
return nil
}
@如何调用
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let startVC = vc(atIndex: 0) {
setViewControllers([startVC], direction: .forward, animated: true, completion: nil)
}
}
三、添加页码
// MARK: - 添加页码
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return headings.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}