ViewController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. var sectionCollection: UICollectionView!
  11. @IBOutlet weak var containerViewLocation: UIView!
  12. @IBOutlet weak var containerViewHome: UIView!
  13. override func viewDidLoad() {
  14. print("LOAD")
  15. super.viewDidLoad()
  16. // Do any additional setup after loading the view, typically from a nib.
  17. }
  18. override func didReceiveMemoryWarning() {
  19. super.didReceiveMemoryWarning()
  20. // Dispose of any resources that can be recreated.
  21. }
  22. let reuseIdentifier = "cell"
  23. var selected = 0
  24. var items = ["Home", "Location", "La Blanca", "Actividades", "Blog", "Gallery"]
  25. // MARK: - UICollectionViewDataSource protocol
  26. //Tell the collection view how many cells to make
  27. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  28. return self.items.count
  29. }
  30. //Make a cell for each section
  31. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  32. print("SET: \(selected)/\(indexPath.item)")
  33. //Get a reference to our storyboard cell
  34. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  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. sectionCollection = collectionView
  51. print("You selected cell #\(indexPath.item)!")
  52. showComponent(selected: indexPath.item)
  53. }
  54. @IBAction func showComponent(selected: Int) {
  55. print("SELECTED: \(selected)")
  56. //Activate label
  57. var i = 0
  58. for cell in sectionCollection.visibleCells as! [MenuCollectionViewCell]{
  59. if i == selected + 1{
  60. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  61. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  62. }
  63. else{
  64. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  65. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  66. }
  67. i = i + 1
  68. }
  69. //Show the view
  70. if selected == 0 {
  71. UIView.animate(withDuration: 0.5, animations: {
  72. self.containerViewHome.alpha = 1
  73. self.containerViewLocation.alpha = 0
  74. })
  75. } else if selected == 1 {
  76. UIView.animate(withDuration: 0.5, animations: {
  77. self.containerViewHome.alpha = 0
  78. self.containerViewLocation.alpha = 1
  79. })
  80. }
  81. }
  82. }