项目介绍:
简单说明
UICollectionView 横向布局 分页 item从左至右排布
最近项目用到横向分页的布局,所以写了这个简单的横向布局Demo
横向布局核心代码
// 计算itemframe
- (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;
}
github地址:https://github.com/dhl613/HLHorizontalPageLayoutDemo