Skip to content

Instantly share code, notes, and snippets.

@thomsmed
Last active December 20, 2021 15:58
Show Gist options
  • Select an option

  • Save thomsmed/477fafadaf05e43c60ce3d13916a51f6 to your computer and use it in GitHub Desktop.

Select an option

Save thomsmed/477fafadaf05e43c60ce3d13916a51f6 to your computer and use it in GitHub Desktop.
TableViewController.swift - From "Transition images to full screen animated" @ medium.com
//
// TableViewController.swift
//
import UIKit
typealias Octocat = (name: String, imagePath: String)
extension String {
func capitalizeFirstLetter() -> String {
return prefix(1).uppercased() + self.lowercased().dropFirst()
}
mutating func capitalizeFirstLetter() {
self = self.capitalizeFirstLetter()
}
}
class TableViewController: UITableViewController {
// Octocats downloaded from https://octodex.github.com/, and bundled together with the app as raw resources
private let octocats: [Octocat] = Bundle.main.paths(forResourcesOfType: nil,
inDirectory: "octocats").map { imagePath in
let name = String(imagePath.split(separator: "/").last?.split(separator: ".").first ?? "")
.capitalizeFirstLetter()
return Octocat(name: name, imagePath: imagePath)
}
private var fullScreenTransitionManager: FullScreenTransitionManager?
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorStyle = .none
tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
}
}
// MARK: UITableViewDelegate, UITableViewDataSource
extension TableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
octocats.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? TableViewCell
else {
return UITableViewCell()
}
let tag = indexPath.row + 1 // Default value for UIView.tag is 0
let octocat = octocats[indexPath.row]
cell.setup(with: octocat, and: tag)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tag = indexPath.row + 1
let octocat = octocats[indexPath.row]
let fullScreenTransitionManager = FullScreenTransitionManager(anchorViewTag: tag)
let fullScreenImageViewController = FullScreenImageViewController(octocat: octocat, tag: tag)
fullScreenImageViewController.modalPresentationStyle = .custom
fullScreenImageViewController.transitioningDelegate = fullScreenTransitionManager
present(fullScreenImageViewController, animated: true)
self.fullScreenTransitionManager = fullScreenTransitionManager
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment