NCLayout.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 preferenceWidth: CGFloat = 110
  57. var marginLeftRight: CGFloat = 1
  58. var itemForLine: CGFloat = 3
  59. override init() {
  60. super.init()
  61. sectionHeadersPinToVisibleBounds = false
  62. minimumInteritemSpacing = 1
  63. minimumLineSpacing = 1
  64. self.scrollDirection = .vertical
  65. self.sectionInset = UIEdgeInsets(top: 10, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  66. }
  67. required init?(coder aDecoder: NSCoder) {
  68. fatalError("init(coder:) has not been implemented")
  69. }
  70. override var itemSize: CGSize {
  71. get {
  72. if let collectionView = collectionView {
  73. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  74. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  75. return CGSize(width: itemWidth, height: itemHeight)
  76. }
  77. // Default fallback
  78. return CGSize(width: 100, height: 100)
  79. }
  80. set {
  81. super.itemSize = newValue
  82. }
  83. }
  84. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  85. return proposedContentOffset
  86. }
  87. }
  88. class NCGridMediaLayout: UICollectionViewFlowLayout {
  89. var increasing = true
  90. var itemForLine: CGFloat = 3
  91. var marginLeftRight: CGFloat = 6
  92. override init() {
  93. super.init()
  94. sectionHeadersPinToVisibleBounds = false
  95. minimumInteritemSpacing = 0
  96. minimumLineSpacing = marginLeftRight
  97. self.scrollDirection = .vertical
  98. self.sectionInset = UIEdgeInsets(top: 0, left: marginLeftRight, bottom: 0, right: marginLeftRight)
  99. }
  100. required init?(coder aDecoder: NSCoder) {
  101. fatalError("init(coder:) has not been implemented")
  102. }
  103. override var itemSize: CGSize {
  104. get {
  105. if let collectionView = collectionView {
  106. let itemWidth: CGFloat = (collectionView.frame.width - marginLeftRight * 2 - marginLeftRight * (itemForLine - 1)) / itemForLine
  107. let itemHeight: CGFloat = itemWidth
  108. return CGSize(width: itemWidth, height: itemHeight)
  109. }
  110. // Default fallback
  111. return CGSize(width: 100, height: 100)
  112. }
  113. set {
  114. super.itemSize = newValue
  115. }
  116. }
  117. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  118. return proposedContentOffset
  119. }
  120. }
  121. extension UICollectionView {
  122. var centerPoint : CGPoint {
  123. get {
  124. return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
  125. }
  126. }
  127. var centerCellIndexPath: IndexPath? {
  128. if let centerIndexPath: IndexPath = self.indexPathForItem(at: self.centerPoint) {
  129. return centerIndexPath
  130. }
  131. return nil
  132. }
  133. }