lazy var orderedViewControllers: [UIViewController] = {
return [self.newViewController(viewController: "firstSB"),
self.newViewController(viewController: "secondSB"),
self.newViewController(viewController: "thirdSB"),
self.newViewController(viewController: "lastSB")]
}()
var PageControl = UIPageControl()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
if let FirstViewController = orderedViewControllers.first {
setViewControllers([FirstViewController],
direction: .forward,
animated: true,
completion: nil)
}
self.delegate = self
ConfigurePageControl()
}
//Создаем функцию, которая отображает навигацию снизу
func ConfigurePageControl() {
PageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 50, width: UIScreen.main.bounds.width, height: 50))
PageControl.numberOfPages = orderedViewControllers.count
PageControl.currentPage = 0
PageControl.tintColor = UIColor.white
PageControl.pageIndicatorTintColor = UIColor.black
PageControl.currentPageIndicatorTintColor = UIColor.white
self.view.addSubview(PageControl)
}
func newViewController(viewController: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
}
//Обрабатываем свайпы назад
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let ViewControllerIndex = orderedViewControllers.index(of: viewController)
else {
return nil
}
let PreviousIndex = ViewControllerIndex - 1
guard PreviousIndex >= 0 else {
return nil
}
guard orderedViewControllers.count > PreviousIndex else {
return nil
}
return orderedViewControllers[PreviousIndex]
}
//Обрабатываем свайпы вперед
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let ViewControllerIndex = orderedViewControllers.index(of: viewController)
else {
return nil
}
let NextIndex = ViewControllerIndex + 1
guard orderedViewControllers.count != NextIndex else {
return nil
}
guard orderedViewControllers.count > NextIndex else {
return nil
}
return orderedViewControllers[NextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let PageContentViewController = pageViewController.viewControllers![0]
self.PageControl.currentPage = orderedViewControllers.index(of: PageContentViewController)!
}