QRPageController.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import UIKit
  2. import DcCore
  3. class QRPageController: UIPageViewController {
  4. private let dcContext: DcContext
  5. var selectedIndex: Int = 0
  6. private lazy var qrController: QrViewController = {
  7. let controller = QrViewController(dcContext: dcContext)
  8. return controller
  9. }()
  10. private lazy var qrCameraController: UIViewController = {
  11. let vc = UIViewController()
  12. vc.view.backgroundColor = .green
  13. return vc
  14. }()
  15. private lazy var qrSegmentControl: UISegmentedControl = {
  16. let control = UISegmentedControl(items: ["Show Left", "Show Right"])
  17. control.tintColor = DcColors.primary
  18. control.addTarget(self, action: #selector(qrSegmentControlChanged), for: .valueChanged)
  19. return control
  20. }()
  21. init(dcContext: DcContext) {
  22. self.dcContext = dcContext
  23. super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:])
  24. }
  25. required init?(coder: NSCoder) {
  26. fatalError("init(coder:) has not been implemented")
  27. }
  28. // MARK: - lifecycle
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. dataSource = self
  32. delegate = self
  33. navigationItem.titleView = qrSegmentControl
  34. setViewControllers(
  35. [qrController],
  36. direction: .forward,
  37. animated: true,
  38. completion: nil
  39. )
  40. }
  41. // MARK: - actions
  42. @objc private func qrSegmentControlChanged(_ sender: UISegmentedControl) {
  43. if sender.selectedSegmentIndex == 0 {
  44. setViewControllers([qrController], direction: .reverse, animated: true, completion: nil)
  45. } else {
  46. setViewControllers([qrCameraController], direction: .forward, animated: true, completion: nil)
  47. }
  48. }
  49. }
  50. // MARK: - UIPageViewControllerDataSource, UIPageViewControllerDelegate
  51. extension QRPageController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {
  52. func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  53. if viewController is QrViewController {
  54. return nil
  55. }
  56. return qrController
  57. }
  58. func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  59. if viewController is QrViewController {
  60. return qrCameraController
  61. }
  62. return nil
  63. }
  64. func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
  65. if completed {
  66. if previousViewControllers.first is QrViewController {
  67. qrSegmentControl.selectedSegmentIndex = 1
  68. } else {
  69. qrSegmentControl.selectedSegmentIndex = 0
  70. }
  71. }
  72. }
  73. }