Tusker/Tusker/Screens/Utilities/SegmentedPageViewController...

98 lines
3.3 KiB
Swift
Raw Normal View History

//
// SegmentedPageViewController.swift
// Tusker
//
// Created by Shadowfacts on 9/13/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
class SegmentedPageViewController: UIPageViewController, UIPageViewControllerDelegate {
let titles: [String]
let pageControllers: [UIViewController]
private(set) var currentIndex: Int!
var segmentedControl: UISegmentedControl!
init(titles: [String], pageControllers: [UIViewController]) {
self.titles = titles
self.pageControllers = pageControllers
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
self.delegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
segmentedControl = UISegmentedControl(items: titles)
segmentedControl.addTarget(self, action: #selector(segmentedControlChanged), for: .valueChanged)
navigationItem.titleView = segmentedControl
segmentedControl.selectedSegmentIndex = 0
selectPage(at: 0, animated: false)
2020-11-15 03:26:02 +00:00
addKeyCommand(MenuController.prevSubTabCommand())
addKeyCommand(MenuController.nextSubTabCommand())
}
func selectPage(at index: Int, animated: Bool) {
let direction: UIPageViewController.NavigationDirection = currentIndex == nil ? .forward : index - currentIndex > 0 ? .forward : .reverse
setViewControllers([pageControllers[index]], direction: direction, animated: animated)
navigationItem.title = pageControllers[index].title
currentIndex = index
2020-11-15 03:26:02 +00:00
segmentedControl.selectedSegmentIndex = index
}
@objc func segmentedControlChanged() {
selectPage(at: segmentedControl.selectedSegmentIndex, animated: true)
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}
// MARK: - Page View Controller Delegate
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
currentIndex = pageControllers.firstIndex(of: viewControllers!.first!)!
segmentedControl.selectedSegmentIndex = currentIndex
navigationItem.title = viewControllers!.first!.title
}
}
extension SegmentedPageViewController: TabBarScrollableViewController {
func tabBarScrollToTop() {
if let scrollableVC = pageControllers[currentIndex] as? TabBarScrollableViewController {
scrollableVC.tabBarScrollToTop()
}
}
}
2020-11-15 03:26:02 +00:00
extension SegmentedPageViewController: TabbedPageViewController {
func selectNextPage() {
guard currentIndex < pageControllers.count - 1 else { return }
selectPage(at: currentIndex + 1, animated: true)
}
func selectPrevPage() {
guard currentIndex > 0 else { return }
selectPage(at: currentIndex - 1, animated: true)
}
}
extension SegmentedPageViewController: BackgroundableViewController {
func sceneDidEnterBackground() {
if let current = pageControllers[currentIndex] as? BackgroundableViewController {
current.sceneDidEnterBackground()
}
}
}