Files
yuyin_ios/SweetParty/主类/旧项目依赖(以后调整)/HLHorizontalPageLayout.m

117 lines
4.3 KiB
Mathematica
Raw Normal View History

2025-08-08 11:05:33 +08:00
//
// HLHorizontalPageLayout.m
// HLHorizontalPageLayoutDemo
//
// Created by dhl613 on 2018/8/24.
// Copyright © 2018 dhl613. All rights reserved.
//
#import "HLHorizontalPageLayout.h"
@interface HLHorizontalPageLayout()
@property (strong, nonatomic) NSMutableArray<UICollectionViewLayoutAttributes *> *layoutAttributes;
/** */
@property (assign, nonatomic) NSInteger rowCount;
/** */
@property (assign, nonatomic) NSInteger columnCount;
@end
@implementation HLHorizontalPageLayout
#pragma mark - Layout
//
- (void)prepareLayout {
[self.layoutAttributes removeAllObjects];
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
//
for (NSInteger i = 0; i < itemCount; i++) {
NSIndexPath *indexpath = [NSIndexPath indexPathForItem:i inSection:0];
UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:indexpath];
[self.layoutAttributes addObject:attr];
}
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attri = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
NSInteger item = indexPath.item;
//
NSInteger pageNumber = item / (self.rowCount * self.columnCount);
// item
NSInteger itemInPage = item % (self.rowCount * self.columnCount);
// item
NSInteger col = itemInPage % self.columnCount;
NSInteger row = itemInPage / self.columnCount;
CGFloat x = self.sectionInset.left + (self.itemSize.width + self.minimumInteritemSpacing)*col + pageNumber * self.collectionView.bounds.size.width;
CGFloat y = self.sectionInset.top + (self.itemSize.height + self.minimumLineSpacing)*row ;
attri.frame = CGRectMake(x, y, self.itemSize.width, self.itemSize.height);
return attri;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
return self.layoutAttributes;
}
- (CGSize)collectionViewContentSize {
NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
NSInteger pageNumber = itemCount / (self.rowCount * self.columnCount);
if (itemCount%(self.rowCount*self.columnCount)) {
pageNumber += 1;
}
return CGSizeMake(pageNumber * self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
}
#pragma mark - Getter
- (NSMutableArray<UICollectionViewLayoutAttributes *> *)layoutAttributes {
if (_layoutAttributes == nil) {
_layoutAttributes = [NSMutableArray array];
}
return _layoutAttributes;
}
// itemSize lineSpacing sectionInsets collectionView frame
- (NSInteger)rowCount {
if (_rowCount == 0) {
NSInteger numerator = self.collectionView.bounds.size.height - self.sectionInset.top - self.sectionInset.bottom + self.minimumLineSpacing;
NSInteger denominator = self.minimumLineSpacing + self.itemSize.height;
NSInteger count = numerator/denominator;
_rowCount = count;
// minimumLineSpacing itemSize.height minimumLineSpacing
if (numerator % denominator) {
self.minimumLineSpacing = (self.collectionView.bounds.size.height - self.sectionInset.top-self.sectionInset.bottom - count * self.itemSize.height) / (CGFloat)(count - 1);
}
}
return _rowCount;
}
// itemSize minimumInteritemSpacing sectionInsets collectionView frame
- (NSInteger)columnCount {
if (_columnCount == 0) {
NSInteger numerator = self.collectionView.bounds.size.width - self.sectionInset.left - self.sectionInset.right + self.minimumInteritemSpacing;
NSInteger denominator = self.minimumInteritemSpacing + self.itemSize.width;
NSInteger count = numerator/denominator;
_columnCount = count;
// minimumInteritemSpacing itemSize.width minimumInteritemSpacing
if (numerator % denominator) {
self.minimumInteritemSpacing = (self.collectionView.bounds.size.width - self.sectionInset.left - self.sectionInset.right - count * self.itemSize.width) / (CGFloat)(count - 1);
}
}
return _columnCount;
}
@end