Guest User

Untitled

a guest
Jan 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import UIKit
  2.  
  3. protocol PinterestLayoutDelegate: class {
  4. func collectionView(_ collectionView:UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat
  5. }
  6.  
  7. class PinterestLayout: UICollectionViewLayout {
  8.  
  9. // 1
  10. weak var delegate: PinterestLayoutDelegate!
  11.  
  12. // 2
  13. var numberOfColumns: Int = 2
  14.  
  15. fileprivate var cellPadding: CGFloat = 6
  16.  
  17. // 3
  18. fileprivate var cache = [UICollectionViewLayoutAttributes]()
  19.  
  20. // 4
  21. fileprivate var contentHeight: CGFloat = 0
  22.  
  23. fileprivate var contentWidth: CGFloat {
  24. guard let collectionView = collectionView else {
  25. return 0
  26. }
  27. let insets = collectionView.contentInset
  28. return collectionView.bounds.width - (insets.left + insets.right)
  29. }
  30.  
  31. // 5
  32. override var collectionViewContentSize: CGSize {
  33. return CGSize(width: contentWidth, height: contentHeight)
  34. }
  35.  
  36. override func prepare() {
  37. // 1
  38. guard cache.isEmpty == true, let collectionView = collectionView else {
  39. return
  40. }
  41. // 2
  42. let columnWidth = contentWidth / CGFloat(numberOfColumns)
  43. var xOffset = [CGFloat]()
  44. for column in 0 ..< numberOfColumns {
  45. xOffset.append(CGFloat(column) * columnWidth)
  46. }
  47. var column = 0
  48. var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)
  49.  
  50. // 3
  51. for item in 0 ..< collectionView.numberOfItems(inSection: 0) {
  52.  
  53. let indexPath = IndexPath(item: item, section: 0)
  54.  
  55. // 4
  56. let photoHeight = delegate.collectionView(collectionView, heightForPhotoAtIndexPath: indexPath)
  57. let height = cellPadding * 2 + photoHeight
  58. let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height)
  59. let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
  60. print("frame",frame)
  61. print("insetFrame",insetFrame)
  62.  
  63. // 5
  64. let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
  65. attributes.frame = insetFrame
  66. cache.append(attributes)
  67.  
  68. // 6
  69. contentHeight = max(contentHeight, frame.maxY)
  70. yOffset[column] = yOffset[column] + height
  71.  
  72. column = column < (numberOfColumns - 1) ? (column + 1) : 0
  73. }
  74. }
  75.  
  76. override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  77.  
  78. var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]()
  79.  
  80. // Loop through the cache and look for items in the rect
  81. for attributes in cache {
  82. if attributes.frame.intersects(rect) {
  83. visibleLayoutAttributes.append(attributes)
  84. }
  85. }
  86. return visibleLayoutAttributes
  87. }
  88.  
  89. override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
  90. return cache[indexPath.item]
  91. }
  92. }
Add Comment
Please, Sign In to add comment