Skip to content

Instantly share code, notes, and snippets.

@VojtaStavik
Last active August 13, 2023 07:32
Show Gist options
  • Save VojtaStavik/20fd3d9f5ef321356477ef1d0c69de1d to your computer and use it in GitHub Desktop.
Save VojtaStavik/20fd3d9f5ef321356477ef1d0c69de1d to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
])
}
let names = ["John", "Paul", "George", "Ringo"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = names[indexPath.row % 4]
return cell
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
for cell in tableView.visibleCells {
let y = tableView.convert(cell.frame.origin, to: view).y
UIView.animate(withDuration: 0.2) {
if y > 40 && y < 280 {
if cell.transform == .identity {
cell.transform = .init(translationX: 40, y: 0)
}
} else {
cell.transform = .identity
}
}
}
}
}
@jetrois
Copy link

jetrois commented Nov 3, 2017

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment