Guest User

Untitled

a guest
Feb 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import UIKit
  2.  
  3. open class ExpandedLayout: UICollectionViewLayout {
  4. public var itemHeight: CGFloat = 50
  5. public var spacing: CGFloat = 10
  6.  
  7. override open func prepare() {
  8. super.prepare()
  9. self.width = self.collectionView?.bounds.width ?? 0
  10. self.numberOfItems = self.collectionView?.numberOfItems(inSection: 0) ?? 0
  11. }
  12.  
  13. override open var collectionViewContentSize: CGSize {
  14. let height = self.frame(for: IndexPath(item: self.numberOfItems, section: 0)).maxY
  15. return CGSize(width: self.width, height: height)
  16. }
  17.  
  18. override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
  19. {
  20. let attributes: [UICollectionViewLayoutAttributes] = (0..<self.numberOfItems).flatMap {
  21. let indexPath = IndexPath(item: $0, section: 0)
  22. let frame = self.frame(for: indexPath)
  23. if !frame.intersects(rect) {
  24. return nil
  25. }
  26. return self.layoutAttributesForItem(at: indexPath)
  27. }
  28. return attributes
  29. }
  30.  
  31. open func frame(for indexPath: IndexPath) -> CGRect {
  32. return CGRect(
  33. x: 0,
  34. y: CGFloat(indexPath.item) * (self.itemHeight + self.spacing),
  35. width: self.collectionView?.bounds.width ?? 0,
  36. height: self.itemHeight)
  37. }
  38.  
  39. override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
  40. let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
  41. attributes.frame = self.frame(for: indexPath)
  42. return attributes
  43. }
  44.  
  45. private var width: CGFloat = 0
  46. private var numberOfItems = 0
  47. }
Add Comment
Please, Sign In to add comment