如果UIPageViewController 的效果是翻页效果 那么下面的点是不会出现的
import UIKit
class PageViewController: UIPageViewController {
private lazy var vc1 = storyboard!.instantiateViewController(withIdentifier: "VC1")
private lazy var vc2 = storyboard!.instantiateViewController(withIdentifier: "VC2")
private lazy var vc3 = storyboard!.instantiateViewController(withIdentifier: "VC3")
private var pages: [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
pages = [vc1,vc2,vc3]
//设置首页
setViewControllers([vc1], direction: .forward, animated: true, completion: nil)
}
}
extension PageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of: viewController)!
return currentIndex == 0 ? pages[pages.count - 1] : pages[currentIndex - 1]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of: viewController)!
return currentIndex == pages.count - 1 ? pages[0] : pages[currentIndex + 1]
}
//显示下面的点
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
}
不使用PageController的点 使用PageControl
class PageViewController: UIPageViewController {
private lazy var vc1 = storyboard!.instantiateViewController(withIdentifier: "VC1")
private lazy var vc2 = storyboard!.instantiateViewController(withIdentifier: "VC2")
private lazy var vc3 = storyboard!.instantiateViewController(withIdentifier: "VC3")
private var pages: [UIViewController] = []
var pageControl: UIPageControl!
var pendingIndex = 1
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
delegate = self
pages = [vc1,vc2,vc3]
//设置首页
setViewControllers([vc1], direction: .forward, animated: true, completion: nil)
pageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 100, width: UIScreen.main.bounds.width, height: 50))
pageControl.numberOfPages = pages.count
// pageControl.currentPage = 0
view.addSubview(pageControl)
}
}
extension PageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of: viewController)!
return currentIndex == 0 ? pages[pages.count - 1] : pages[currentIndex - 1]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of: viewController)!
return currentIndex == pages.count - 1 ? pages[0] : pages[currentIndex + 1]
}
}
extension PageViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
pageControl.currentPage = pendingIndex
}
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
pendingIndex = pages.firstIndex(of: pendingViewControllers[0])!
}
}
网友评论