Advertisement
Don_Mag

Page View Controller in SwiftUI

Aug 15th, 2024
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.91 KB | None | 0 0
  1. import SwiftUI
  2. import UIKit
  3.  
  4. class SomePageVC: UIViewController {
  5.     let theLabel = UILabel()
  6.     override func viewDidLoad() {
  7.         super.viewDidLoad()
  8.         theLabel.translatesAutoresizingMaskIntoConstraints = false
  9.         view.addSubview(theLabel)
  10.         NSLayoutConstraint.activate([
  11.             theLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  12.             theLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
  13.         ])
  14.         theLabel.font = .systemFont(ofSize: 60.0, weight: .light)
  15.     }
  16. }
  17. class CustomPageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  18.    
  19.     func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
  20.  
  21.         guard let index = viewControllersList.firstIndex(of: viewController), index > 0 else {
  22.             return nil
  23.         }
  24.         if let vc = viewControllersList[index - 1] as? SomePageVC {
  25.             print("viewControllerBefore:", vc.theLabel.text)
  26.         }
  27.         return viewControllersList[index - 1]
  28.        
  29.     }
  30.    
  31.     func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
  32.        
  33.         guard let index = viewControllersList.firstIndex(of: viewController), index < viewControllersList.count - 1 else {
  34.             return nil
  35.         }
  36.         if let vc = viewControllersList[index + 1] as? SomePageVC {
  37.             print("viewControllerAfter:", vc.theLabel.text)
  38.         }
  39.         return viewControllersList[index + 1]
  40.  
  41.     }
  42.    
  43.     lazy var viewControllersList: [UIViewController] = []
  44.    
  45.     override func viewDidLoad() {
  46.         super.viewDidLoad()
  47.        
  48.         self.dataSource = self
  49.         self.delegate = self
  50.        
  51.         let colors: [UIColor] = [
  52.             .red, .green, .systemBlue, .yellow, .cyan, .lightGray,
  53.         ]
  54.         for i in 0..<6 {
  55.             let vc = SomePageVC()
  56.             vc.view.backgroundColor = colors[i % colors.count]
  57.             vc.theLabel.text = "Page \(i)"
  58.             viewControllersList.append(vc)
  59.         }
  60.        
  61.         // if we have an odd number of "pages" we need to add a blank page at the end
  62.         if viewControllersList.count % 2 == 1 {
  63.             let vc = SomePageVC()
  64.             vc.view.backgroundColor = .systemBackground
  65.             vc.theLabel.text = "Blank"
  66.             viewControllersList.append(vc)
  67.         }
  68.        
  69.         if self.spineLocation == .mid {
  70.             setViewControllers([viewControllersList[0], viewControllersList[1]], direction: .forward, animated: true)
  71.         } else {
  72.             setViewControllers([viewControllersList[0]], direction: .forward, animated: true)
  73.         }
  74.        
  75.     }
  76.    
  77.     func pageViewController(_ pageViewController: UIPageViewController, spineLocationFor interfaceOrientation: UIInterfaceOrientation) -> UIPageViewController.SpineLocation {
  78.  
  79.         // safely unwrap optionals
  80.         guard let curVC = pageViewController.viewControllers?.first, let idx = viewControllersList.firstIndex(of: curVC)
  81.         else { fatalError("This should never happen!") }
  82.        
  83.         if interfaceOrientation.isLandscape {
  84.             pageViewController.isDoubleSided = true
  85.             let newIDX = (idx % 2 == 1) ? idx - 1 : idx
  86.             setViewControllers([viewControllersList[newIDX], viewControllersList[newIDX + 1]], direction: .forward, animated: true, completion: nil)
  87.         } else {
  88.             pageViewController.isDoubleSided = false
  89.             setViewControllers([viewControllersList[idx]], direction: .forward, animated: true, completion: nil)
  90.         }
  91.        
  92.         return interfaceOrientation.isLandscape ? .mid : .min
  93.        
  94.     }
  95. }
  96.  
  97. // 2. PageViewControllerRepresentable
  98. struct MyViewControllerRepresentable: UIViewControllerRepresentable {
  99.    
  100.     func makeUIViewController(context: Context) -> CustomPageViewController {
  101.         let pageViewController = CustomPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal)
  102.         return pageViewController
  103.     }
  104.    
  105.     func updateUIViewController(_ uiViewController: CustomPageViewController, context: Context) {
  106.         // Update the UIPageViewController if needed
  107.     }
  108.    
  109. }
  110.  
  111. struct TestView : View {
  112.     var body: some View{
  113.         VStack{
  114.             MyViewControllerRepresentable()
  115.         }
  116.     }
  117. }
  118.  
  119. #Preview {
  120.     TestView()
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement