У меня есть пользовательский подкласс UICollectionViewFlowLayout
, который отображает все ячейки друг над другом (таким образом, пользователю видна только самая верхняя ячейка). Я полагаю, поскольку все представления в рамке, UICollectionView
отображает все ячейки, даже если для пользователя фактически видна только одна.
Есть ли способ повлиять на количество ячеек, отображаемых UICollectionView
? Я бы предпочел отображать только первые две или три ячейки.
class UICollectionViewStackLayout: UICollectionViewFlowLayout {
override init() {
super.init()
self.scrollDirection = .vertical
self.minimumInteritemSpacing = 0
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard
let attributes = super.layoutAttributesForElements(in: rect),
let collectionView = self.collectionView
else {
return nil
}
for attribute in attributes {
attribute.zIndex = Int.max - attribute.indexPath.item // First item on top
attribute.center = collectionView.center
}
return attributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return UICollectionViewLayoutAttributes(forCellWith: indexPath)
}
}