2019-09-14 16:04:06 +00:00
|
|
|
//
|
|
|
|
// SegmentedPageViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/13/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2019-09-14 19:59:17 +00:00
|
|
|
class SegmentedPageViewController: UIPageViewController, UIPageViewControllerDelegate {
|
2019-09-14 16:04:06 +00:00
|
|
|
|
|
|
|
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())
|
2019-09-14 16:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-09-14 16:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func segmentedControlChanged() {
|
|
|
|
selectPage(at: segmentedControl.selectedSegmentIndex, animated: true)
|
2019-09-16 17:14:58 +00:00
|
|
|
UIImpactFeedbackGenerator(style: .light).impactOccurred()
|
2019-09-14 16:04:06 +00:00
|
|
|
}
|
2019-09-14 19:59:17 +00:00
|
|
|
|
2019-09-14 16:04:06 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-07-03 23:36:48 +00:00
|
|
|
|
|
|
|
extension SegmentedPageViewController: TabBarScrollableViewController {
|
|
|
|
func tabBarScrollToTop() {
|
|
|
|
if let scrollableVC = pageControllers[currentIndex] as? TabBarScrollableViewController {
|
|
|
|
scrollableVC.tabBarScrollToTop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 02:55:58 +00:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:55:58 +00:00
|
|
|
extension SegmentedPageViewController: BackgroundableViewController {
|
|
|
|
func sceneDidEnterBackground() {
|
|
|
|
if let current = pageControllers[currentIndex] as? BackgroundableViewController {
|
|
|
|
current.sceneDidEnterBackground()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|