Guest User

Untitled

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import UIKit
  2. import Fakery
  3.  
  4. class ViewController: UIViewController {
  5.  
  6.  
  7. @IBOutlet weak var tableView: UITableView!
  8.  
  9. private var companies = [String]()
  10. private var panGesture: UIPanGestureRecognizer!
  11.  
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. collapseTableView()
  15. panGesture = UIPanGestureRecognizer(target: self, action: #selector(panTableView))
  16. tableView.addGestureRecognizer(panGesture)
  17. panGesture.delegate = self
  18. tableView.dataSource = self
  19.  
  20. let faker = Faker(locale: "nb-NO")
  21.  
  22. for _ in 0...50 {
  23. companies.append(faker.company.name())
  24. }
  25. }
  26.  
  27. @objc private func panTableView(gesture: UIPanGestureRecognizer) {
  28. print("panTableView: \(tableView.frame)")
  29. let translation = panGesture.translation(in: view)
  30. panGesture.view?.center = CGPoint(x: panGesture.view!.center.x, y: panGesture.view!.center.y + translation.y)
  31. panGesture.setTranslation(CGPoint.zero, in: view)
  32. if tableView.frame.origin.y <= 0.0 {
  33. tableView.frame = view.bounds
  34. }
  35. }
  36.  
  37. @IBAction private func collapseTableView() {
  38.  
  39. UIView.animate(withDuration: 0.5) {
  40. self.tableView.frame = CGRect(x: 0,
  41. y: self.view.bounds.height * 0.75,
  42. width: self.view.bounds.width,
  43. height: self.view.bounds.height)
  44. }
  45. }
  46. }
  47.  
  48. extension ViewController: UIScrollViewDelegate {
  49. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  50. print("scrollViewDidScroll")
  51. }
  52. }
  53.  
  54. extension ViewController: UIGestureRecognizerDelegate {
  55. func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
  56. print("gestureRecognizerShouldBegin")
  57. print("tableview frame: \(tableView.frame)")
  58. if tableView.frame.origin.y <= 0.0 {
  59. return false
  60. }
  61. return true
  62. }
  63. }
  64.  
  65. extension ViewController: UITableViewDataSource {
  66. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  67. return companies.count
  68. }
  69. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  70. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  71. cell.textLabel?.text = companies[indexPath.row]
  72. return cell
  73. }
  74. }
Add Comment
Please, Sign In to add comment