Swift 翻页控制器UIPageViewController
1.在StoryBoard中拖入UIPageViewController
2.在StoryBoard中拖入UIView作为引导页
3.为UIPageViewController添加StoryBoardID(此处貌似是为了控制第一次打开app时展示)
4.建立两个类(分别继承自UIPageViewController, UIViewController),并在StoryBoard中为视图模型添加相应的控制器属性
5.在UIPageViewController.swift文件中设置数据源为自身
dataSource = self
6.初始化第一个界面
if let startVC = initVC(atIndex: 0) {
setViewController([startVC], direction: .forward, animated: true, completion: nil)
}
7.在UIPageViewController.swift文件中实现UIPageViewControllerDataSource的协议
8.完成UIPageViewControllerDataSource的两个方法(上一个,下一个控制器分别为啥)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {}
9.自定义方法生成引导页面(此处用的UIViewController)
10.在APP第一个页面的viewWillAppear中添加引导页面
override func viewWillAppear(_ animated: Bool) {
if let pageVC = storyboard?.instantiateViewController(withIdentifier: "GuideController") as?
GuidePageViewController {
present(pageVC, animated: true, completion: nil)
}
}
11.添加翻页控制器页码
StoryBoard中拖一个pageControl到View界面,并Outlet到相应swfit文件中
12.设定页码为当前控制器的索引
pageController.currentPage = index