Last active
August 3, 2024 04:10
-
-
Save marlonjames71/e8d14e211be84cd344075d3a46e52e5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PagedMediaViewController: UIPageViewController { | |
// MARK: - Properties | |
private var mediaItems: [MediaItem] | |
private var currentIndex: Int | |
// MARK: - Init | |
init(mediaItems: [MediaItem], currentIndex: Int) { | |
self.mediaItems = mediaItems | |
self.currentIndex = currentIndex | |
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) | |
} | |
required init(coder: NSCoder) { | |
fatalError("Use init(mediaItems: [MediaItem], currentIndex: Int)") | |
} | |
// MARK: - Lifecycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
dataSource = self | |
delegate = self | |
if let firstMediaVC = mediaViewControllerAtIndex(currentIndex) { | |
let viewControllers = [firstMediaVC] | |
setViewControllers(viewControllers, direction: .forward, animated: false, completion: nil) | |
} | |
} | |
// MARK: - Helper Methods | |
private func mediaViewControllerAtIndex(_ index: Int) -> MediaViewController? { | |
// returns nil if the index is out of bounds, this way the app won't crash. | |
// You can also put a print statement before you return. | |
guard (0...mediaItems.count).contains(index) else { return nil } | |
let item = mediaItems[index] | |
return switch item.mediaType { | |
case .video: | |
VideoViewController(mediaItem: item) | |
case .photo: | |
PhotoViewController(mediaItem: item) | |
} | |
} | |
} | |
// MARK: - DataSource Methods | |
extension PagedMediaViewController: UIPageViewControllerDataSource { | |
pageViewController( | |
_ pageViewController: UIPageViewController, | |
viewControllerBefore viewController: UIViewController | |
) -> UIViewController? { | |
guard currentIndex > 0 else { return nil } | |
return mediaViewControllerAtIndex(currentIndex - 1) | |
} | |
pageViewController( | |
_ pageViewController: UIPageViewController, | |
viewControllerAfter viewController: UIViewController | |
) -> UIViewController? { | |
guard currentIndex < mediaItems.count - 1 else { return nil } | |
return mediaViewControllerAtIndex(currentIndex + 1) | |
} | |
} | |
// MARK: - Delelgate Methods | |
extension PagedMediaViewController: UIPageViewControllerDelegate { | |
func pageViewController( | |
_ pageViewController: UIPageViewController, | |
didFinishAnimating finished: Bool, | |
previousViewControllers: [UIViewController], | |
transitionCompleted completed: Bool | |
) { | |
guard | |
let mediaVCs = pageViewController.viewControllers as? [MediaViewController], | |
let currIndex = mediaItems.firstIndex(of: mediaVCs[0].mediaItem) | |
else { return } | |
currentIndex = currIndex | |
} | |
// If you want to specify the interface orientations you want to support, implement these as well: | |
func pageViewControllerSupportedInterfaceOrientation( | |
_ pageViewController: UIPageViewController | |
) -> UIInterfaceOrientationMask { | |
// example | |
.allButUpsideDown | |
} | |
func pageViewControllerPreferredInterfaceOrientationForPresentation( | |
_ pageViewController: UIPageViewController | |
) -> UIInterfaceOrientationMask { | |
// example | |
.portrait | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment