ViewController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 containerViewGallery: UIView!
  12. @IBOutlet weak var containerViewBlog: UIView!
  13. @IBOutlet weak var containerViewActivities: UIView!
  14. @IBOutlet weak var containerViewLablanca: UIView!
  15. @IBOutlet weak var containerViewLocation: UIView!
  16. @IBOutlet weak var containerViewHome: UIView!
  17. override func viewDidLoad() {
  18. //Hide all sections, except for the first one
  19. self.containerViewLocation.alpha = 0
  20. self.containerViewLablanca.alpha = 0
  21. self.containerViewActivities.alpha = 0
  22. self.containerViewBlog.alpha = 0
  23. self.containerViewGallery.alpha = 0
  24. super.viewDidLoad()
  25. // Do any additional setup after loading the view, typically from a nib.
  26. }
  27. override func didReceiveMemoryWarning() {
  28. super.didReceiveMemoryWarning()
  29. // Dispose of any resources that can be recreated.
  30. }
  31. let reuseIdentifier = "cell"
  32. var selected = 0
  33. var items = ["Home", "Location", "La Blanca", "Actividades", "Blog", "Gallery"]
  34. // MARK: - UICollectionViewDataSource protocol
  35. //Tell the collection view how many cells to make
  36. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  37. return self.items.count
  38. }
  39. //Make a cell for each section
  40. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  41. print("SET: \(selected)/\(indexPath.item)")
  42. //Get a reference to our storyboard cell
  43. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  44. //Set text
  45. cell.label.text = self.items[indexPath.item]
  46. //Mark the first cell as selected...
  47. if indexPath.item == selected{
  48. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  49. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  50. }
  51. //... and the others as unselected
  52. else{
  53. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  54. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  55. }
  56. return cell
  57. }
  58. // MARK: - UICollectionViewDelegate protocol
  59. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  60. sectionCollection = collectionView
  61. print("You selected cell #\(indexPath.item)!")
  62. showComponent(selected: indexPath.item)
  63. }
  64. @IBAction func showComponent(selected: Int) {
  65. print("SELECTED: \(selected)")
  66. //Activate label
  67. var i = 0
  68. for cell in sectionCollection.visibleCells as! [MenuCollectionViewCell]{ //TODO: Not only visibles!
  69. if i == selected + 1{
  70. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  71. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  72. }
  73. else{
  74. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  75. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  76. }
  77. i = i + 1
  78. }
  79. //Show the view
  80. if selected == 0 {
  81. UIView.animate(withDuration: 0.5, animations: {
  82. self.containerViewHome.alpha = 1
  83. self.containerViewLocation.alpha = 0
  84. self.containerViewLablanca.alpha = 0
  85. self.containerViewActivities.alpha = 0
  86. self.containerViewBlog.alpha = 0
  87. self.containerViewGallery.alpha = 0
  88. })
  89. }
  90. else if selected == 1 {
  91. UIView.animate(withDuration: 0.5, animations: {
  92. self.containerViewHome.alpha = 0
  93. self.containerViewLocation.alpha = 1
  94. self.containerViewLablanca.alpha = 0
  95. self.containerViewActivities.alpha = 0
  96. self.containerViewBlog.alpha = 0
  97. self.containerViewGallery.alpha = 0
  98. })
  99. }
  100. else if selected == 2 {
  101. UIView.animate(withDuration: 0.5, animations: {
  102. self.containerViewHome.alpha = 0
  103. self.containerViewLocation.alpha = 0
  104. self.containerViewLablanca.alpha = 1
  105. self.containerViewActivities.alpha = 0
  106. self.containerViewBlog.alpha = 0
  107. self.containerViewGallery.alpha = 0
  108. })
  109. }
  110. else if selected == 3 {
  111. UIView.animate(withDuration: 0.5, animations: {
  112. self.containerViewHome.alpha = 0
  113. self.containerViewLocation.alpha = 0
  114. self.containerViewLablanca.alpha = 0
  115. self.containerViewActivities.alpha = 1
  116. self.containerViewBlog.alpha = 0
  117. self.containerViewGallery.alpha = 0
  118. })
  119. }
  120. else if selected == 4 {
  121. UIView.animate(withDuration: 0.5, animations: {
  122. self.containerViewHome.alpha = 0
  123. self.containerViewLocation.alpha = 0
  124. self.containerViewLablanca.alpha = 0
  125. self.containerViewActivities.alpha = 0
  126. self.containerViewBlog.alpha = 1
  127. self.containerViewGallery.alpha = 0
  128. })
  129. }
  130. else if selected == 5 {
  131. UIView.animate(withDuration: 0.5, animations: {
  132. self.containerViewHome.alpha = 0
  133. self.containerViewLocation.alpha = 0
  134. self.containerViewLablanca.alpha = 0
  135. self.containerViewActivities.alpha = 0
  136. self.containerViewBlog.alpha = 0
  137. self.containerViewGallery.alpha = 1
  138. })
  139. }
  140. }
  141. }