NCLayout.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // NCLayout.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/11/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. class NCListLayout: UICollectionViewFlowLayout {
  25. let itemHeight: CGFloat = 60
  26. override init() {
  27. super.init()
  28. sectionHeadersPinToVisibleBounds = false
  29. minimumInteritemSpacing = 0
  30. minimumLineSpacing = 1
  31. self.scrollDirection = .vertical
  32. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  33. }
  34. required init?(coder aDecoder: NSCoder) {
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. override var itemSize: CGSize {
  38. get {
  39. if let collectionView = collectionView {
  40. let itemWidth: CGFloat = collectionView.frame.width
  41. return CGSize(width: itemWidth, height: self.itemHeight)
  42. }
  43. // Default fallback
  44. return CGSize(width: 100, height: 100)
  45. }
  46. set {
  47. super.itemSize = newValue
  48. }
  49. }
  50. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  51. return proposedContentOffset
  52. }
  53. }
  54. class NCGridLayout: UICollectionViewFlowLayout {
  55. var heightLabelPlusButton: CGFloat = 45
  56. var marginLeftRight: CGFloat = 6
  57. var itemForLine: CGFloat = 3
  58. override init() {
  59. super.init()
  60. sectionHeadersPinToVisibleBounds = false
  61. minimumInteritemSpacing = 1
  62. minimumLineSpacing = marginLeftRight
  63. self.scrollDirection = .vertical
  64. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  65. }
  66. required init?(coder aDecoder: NSCoder) {
  67. fatalError("init(coder:) has not been implemented")
  68. }
  69. override var itemSize: CGSize {
  70. get {
  71. if let collectionView = collectionView {
  72. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  73. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  74. return CGSize(width: itemWidth, height: itemHeight)
  75. }
  76. // Default fallback
  77. return CGSize(width: 100, height: 100)
  78. }
  79. set {
  80. super.itemSize = newValue
  81. }
  82. }
  83. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  84. return proposedContentOffset
  85. }
  86. }
  87. class NCGridMediaLayout: UICollectionViewFlowLayout {
  88. var marginLeftRight: CGFloat = 6
  89. var itemForLine: CGFloat = 3
  90. override init() {
  91. super.init()
  92. sectionHeadersPinToVisibleBounds = false
  93. minimumInteritemSpacing = 0
  94. minimumLineSpacing = marginLeftRight
  95. self.scrollDirection = .vertical
  96. self.sectionInset = UIEdgeInsets(top: 0, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  97. }
  98. required init?(coder aDecoder: NSCoder) {
  99. fatalError("init(coder:) has not been implemented")
  100. }
  101. override var itemSize: CGSize {
  102. get {
  103. if let collectionView = collectionView {
  104. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  105. let itemHeight: CGFloat = itemWidth
  106. return CGSize(width: itemWidth, height: itemHeight)
  107. }
  108. // Default fallback
  109. return CGSize(width: 100, height: 100)
  110. }
  111. set {
  112. super.itemSize = newValue
  113. }
  114. }
  115. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  116. return proposedContentOffset
  117. }
  118. }
  119. extension UICollectionView {
  120. var centerPoint : CGPoint {
  121. get {
  122. return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
  123. }
  124. }
  125. var centerCellIndexPath: IndexPath? {
  126. if let centerIndexPath: IndexPath = self.indexPathForItem(at: self.centerPoint) {
  127. return centerIndexPath
  128. }
  129. return nil
  130. }
  131. }