Last active
July 22, 2022 04:57
-
-
Save marlonjames71/09381c2a3b44ae443ed566b35c3c5c5b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // For one dimensional array | |
| func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
| let item = mediaItems[indexPath.item] | |
| let pagedMediaViewController = PagedMediaController(mediaItem: item, currentIndex: indexPath.item) | |
| pagedMediaViewController.modalPresentationStyle = .overFullScreen | |
| present(pagedMediaViewController, animated: true) | |
| } | |
| // In case you're dealing with a two dimensional array like I was dealing with since the collection view | |
| // had sections | |
| func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
| let item = mediaItems[indexPath.section][indexPath.item] | |
| let joinedItems = mediaItems.flatMap { $0 } | |
| guard let currentIndexForPagedMediaVC = joinedItems.firstIndex(of: item) else { return } | |
| let pagedMediaViewController = PagedMediaController(mediaItem: item, currentIndex: currentIndexForPagedMediaVC) | |
| pagedMediaViewController.modalPresentationStyle = .overFullScreen | |
| present(pagedMediaViewController, animated: true) | |
| } |
This file contains hidden or 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
| 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] | |
| switch item.mediaType { | |
| case .video: | |
| return VideoViewController(mediaItem: item) | |
| case .photo: | |
| return PhotoViewController(mediaItem: item) | |
| } | |
| } |
This file contains hidden or 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 MediaViewController: UIViewController { | |
| let mediaItem: MediaItem | |
| init(mediaItem: MediaItem) { | |
| self.mediaItem = mediaItem | |
| } | |
| required init(coder: NSCoder) { | |
| fatalError("Use init(mediaItem: MediaItem)") | |
| } | |
| // … More code | |
| } |
This file contains hidden or 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 VideoViewController: MediaViewController { | |
| ... | |
| } | |
| class PhotoViewController: MediaViewController { | |
| ... | |
| } |
This file contains hidden or 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
| extension PagedMediaViewController: UIPageViewControllerDataSource { | |
| pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { | |
| return mediaViewControllerAtIndex(currentIndex - 1) | |
| } | |
| pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { | |
| return mediaViewControllerAtIndex(currentIndex + 1) | |
| } | |
| } |
This file contains hidden or 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
| 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 | |
| } | |
| } |
This file contains hidden or 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)") | |
| } | |
| } |
This file contains hidden or 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
| 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) | |
| } | |
| } |
This file contains hidden or 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] | |
| switch item.mediaType { | |
| case .video: | |
| return VideoViewController(mediaItem: item) | |
| case .photo: | |
| return PhotoViewController(mediaItem: item) | |
| } | |
| } | |
| } | |
| // MARK: - DataSource Methods | |
| extension PagedMediaViewController: UIPageViewControllerDataSource { | |
| pageViewController( | |
| _ pageViewController: UIPageViewController, | |
| viewControllerBefore viewController: UIViewController | |
| ) -> UIViewController? { | |
| return mediaViewControllerAtIndex(currentIndex - 1) | |
| } | |
| pageViewController( | |
| _ pageViewController: UIPageViewController, | |
| viewControllerAfter viewController: UIViewController | |
| ) -> UIViewController? { | |
| 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 | |
| } | |
| } |
This file contains hidden or 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
| struct MediaItem { | |
| let id: UUID | |
| let mediaType: MediaType | |
| let title: String? | |
| let mediaUrl: URL? | |
| } | |
| enum MediaType { case video, photo } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment