// // YOUPAIWZHorizontalPageFlowlayout.m // WZShenmaStar // // Created by bigbiao on 2017/5/10. // Copyright © 2017年 BigBiao. All rights reserved. // #import "YOUPAIWZHorizontalPageFlowlayout.h" @implementation YOUPAIWZHorizontalPageFlowlayout #pragma mark - Public - (void)setColumnSpacing:(CGFloat)columnSpacing rowSpacing:(CGFloat)rowSpacing edgeInsets:(UIEdgeInsets)edgeInsets { self.youpaipcolumnSpacing = columnSpacing; self.youpaiprowSpacing = rowSpacing; self.youpaipedgeInsets = edgeInsets; } - (void)setRowCount:(NSInteger)rowCount itemCountPerRow:(NSInteger)itemCountPerRow { self.youpaiprowCount = rowCount; self.youpaipitemCountPerRow = itemCountPerRow; } #pragma mark - 构造方法 + (instancetype)youpaifhorizontalPageFlowlayoutWithRowCount:(NSInteger)rowCount itemCountPerRow:(NSInteger)itemCountPerRow { return [[self alloc] initWithRowCount:rowCount itemCountPerRow:itemCountPerRow]; } - (instancetype)initWithRowCount:(NSInteger)rowCount itemCountPerRow:(NSInteger)itemCountPerRow { self = [super init]; if (self) { self.youpaiprowCount = rowCount; self.youpaipitemCountPerRow = itemCountPerRow; } return self; } #pragma mark - 重写父类方法 - (instancetype)init { self = [super init]; if (self) { [self setColumnSpacing:0 rowSpacing:0 edgeInsets:UIEdgeInsetsZero]; } return self; } /** 布局前做一些准备工作 */ - (void)prepareLayout { [super prepareLayout]; // 从collectionView中获取到有多少个item NSInteger itemTotalCount = [self.collectionView numberOfItemsInSection:0]; // 遍历出item的attributes,把它添加到管理它的属性数组中去 for (int i = 0; i < itemTotalCount; i++) { NSIndexPath *indexpath = [NSIndexPath indexPathForItem:i inSection:0]; UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexpath]; [self.youpaipattributesArrayM addObject:attributes]; } } /** 计算collectionView的滚动范围 */ - (CGSize)collectionViewContentSize { // 计算出item的宽度 CGFloat itemWidth = (self.collectionView.frame.size.width - self.youpaipedgeInsets.left - self.youpaipitemCountPerRow * self.youpaipcolumnSpacing) / self.youpaipitemCountPerRow; // 从collectionView中获取到有多少个item NSInteger itemTotalCount = [self.collectionView numberOfItemsInSection:0]; // 理论上每页展示的item数目 NSInteger itemCount = self.youpaiprowCount * self.youpaipitemCountPerRow; // 余数(用于确定最后一页展示的item个数) NSInteger remainder = itemTotalCount % itemCount; // 除数(用于判断页数) NSInteger pageNumber = itemTotalCount / itemCount; // 总个数小于self.rowCount * self.itemCountPerRow if (itemTotalCount <= itemCount) { pageNumber = 1; }else { if (remainder == 0) { pageNumber = pageNumber; }else { // 余数不为0,除数加1 pageNumber = pageNumber + 1; } } CGFloat width = 0; // 考虑特殊情况(当item的总个数不是self.rowCount * self.itemCountPerRow的整数倍,并且余数小于每行展示的个数的时候) // if (pageNumber > 1 && remainder != 0 && remainder < self.itemCountPerRow) { // width = self.edgeInsets.left + (pageNumber - 1) * self.itemCountPerRow * (itemWidth + self.columnSpacing) + remainder * itemWidth + (remainder - 1)*self.columnSpacing + self.edgeInsets.right; // }else { width = self.youpaipedgeInsets.left + pageNumber * self.youpaipitemCountPerRow * (itemWidth + self.youpaipcolumnSpacing) - self.youpaipcolumnSpacing + self.youpaipedgeInsets.right; // } // 只支持水平方向上的滚动 return CGSizeMake(width, 0); } /** 设置每个item的属性(主要是frame) */ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { // item的宽高由行列间距和collectionView的内边距决定 CGFloat itemWidth = (self.collectionView.frame.size.width - self.youpaipedgeInsets.left - self.youpaipitemCountPerRow * self.youpaipcolumnSpacing) / self.youpaipitemCountPerRow; CGFloat itemHeight = (self.collectionView.frame.size.height - self.youpaipedgeInsets.top - self.youpaipedgeInsets.bottom - (self.youpaiprowCount - 1) * self.youpaiprowSpacing) / self.youpaiprowCount; NSInteger item = indexPath.item; // 当前item所在的页 NSInteger pageNumber = item / (self.youpaiprowCount * self.youpaipitemCountPerRow); NSInteger x = item % self.youpaipitemCountPerRow + pageNumber * self.youpaipitemCountPerRow; NSInteger y = item / self.youpaipitemCountPerRow - pageNumber * self.youpaiprowCount; // 计算出item的坐标 CGFloat itemX = self.youpaipedgeInsets.left + (itemWidth + self.youpaipcolumnSpacing) * x; CGFloat itemY = self.youpaipedgeInsets.top + (itemHeight + self.youpaiprowSpacing) * y; UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath]; // 每个item的frame attributes.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight); return attributes; } /** 返回collectionView视图中所有视图的属性数组 */ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { return self.youpaipattributesArrayM; } #pragma mark - Lazy - (NSMutableArray *)youpaipattributesArrayM { if (!_youpaipattributesArrayM) { _youpaipattributesArrayM = [NSMutableArray array]; } return _youpaipattributesArrayM; } @end