NCLayout.swift 5.2 KB

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