ViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // ViewController.swift
  3. // app
  4. //
  5. // Created by Inigo Valentin on 28/09/16.
  6. // Copyright © 2016 Margolariak. All rights reserved.
  7. //
  8. import UIKit
  9. class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  10. override func viewDidLoad() {
  11. print("LOAD")
  12. super.viewDidLoad()
  13. // Do any additional setup after loading the view, typically from a nib.
  14. }
  15. override func didReceiveMemoryWarning() {
  16. super.didReceiveMemoryWarning()
  17. // Dispose of any resources that can be recreated.
  18. }
  19. let reuseIdentifier = "cell"
  20. var selected = 0
  21. var items = ["Home", "Location", "La Blanca", "Actividades", "Blog", "Gallery"]
  22. // MARK: - UICollectionViewDataSource protocol
  23. //Tell the collection view how many cells to make
  24. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  25. return self.items.count
  26. }
  27. //Make a cell for each section
  28. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  29. print("SET: \(selected)/\(indexPath.item)")
  30. //Get a reference to our storyboard cell
  31. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  32. //Make the updatable
  33. //cell.setNeedsDisplay()
  34. //cell.setNeedsLayout()
  35. //Set text
  36. cell.label.text = self.items[indexPath.item]
  37. //Mark the first cell as selected
  38. if indexPath.item == selected{
  39. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  40. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  41. }
  42. else{
  43. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  44. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  45. }
  46. return cell
  47. }
  48. // MARK: - UICollectionViewDelegate protocol
  49. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  50. // Return all labels to their original state, and mark the selected as such
  51. var i = 0
  52. selected = indexPath.item
  53. for cell in collectionView.visibleCells as! [MenuCollectionViewCell]{
  54. print("\(i)/\(indexPath.item)")
  55. if i == indexPath.item{
  56. print("CHANGE")
  57. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  58. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  59. }
  60. else{
  61. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  62. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  63. }
  64. //cell.superview?.superview?.setNeedsDisplay()
  65. //cell.setNeedsLayout()
  66. //DispatchQueue.main.async{cell.setNeedsDisplay()}
  67. i = i + 1
  68. }
  69. print("REDRAW")
  70. //collectionView.setNeedsDisplay()
  71. collectionView.layoutIfNeeded()
  72. //collectionView.setNeedsLayout()
  73. //DispatchQueue.main.async{collectionView.setNeedsDisplay()}
  74. //dispatch_async(dispatch_get_main_queue(), {collectionView.setNeedsDisplay()})
  75. //Mark the selected one
  76. //let sel = collectionView.visibleCells as! [[MenuCollectionViewCell]indexPath]
  77. //let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  78. print("You selected cell #\(indexPath.item)!")
  79. }
  80. }