Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- open class ExpandedLayout: UICollectionViewLayout {
- public var itemHeight: CGFloat = 50
- public var spacing: CGFloat = 10
- override open func prepare() {
- super.prepare()
- self.width = self.collectionView?.bounds.width ?? 0
- self.numberOfItems = self.collectionView?.numberOfItems(inSection: 0) ?? 0
- }
- override open var collectionViewContentSize: CGSize {
- let height = self.frame(for: IndexPath(item: self.numberOfItems, section: 0)).maxY
- return CGSize(width: self.width, height: height)
- }
- override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
- {
- let attributes: [UICollectionViewLayoutAttributes] = (0..<self.numberOfItems).flatMap {
- let indexPath = IndexPath(item: $0, section: 0)
- let frame = self.frame(for: indexPath)
- if !frame.intersects(rect) {
- return nil
- }
- return self.layoutAttributesForItem(at: indexPath)
- }
- return attributes
- }
- open func frame(for indexPath: IndexPath) -> CGRect {
- return CGRect(
- x: 0,
- y: CGFloat(indexPath.item) * (self.itemHeight + self.spacing),
- width: self.collectionView?.bounds.width ?? 0,
- height: self.itemHeight)
- }
- override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
- let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
- attributes.frame = self.frame(for: indexPath)
- return attributes
- }
- private var width: CGFloat = 0
- private var numberOfItems = 0
- }
Add Comment
Please, Sign In to add comment