實(shí)現(xiàn)了對(duì)UICollectionView橫向滑動(dòng)的時(shí)候UICollectionViewCell橫向排列的功能优妙,并帶有分組功能
先看下效果圖
廢話不多說直接上代碼
#import <UIKit/UIKit.h>
@interface ELCVFlowLayout : UICollectionViewFlowLayout
@end
#import "ELCVFlowLayout.h"
@interface ELCVFlowLayout ()
@property (nonatomic, copy) NSMutableDictionary *sectionDic;
@property (strong, nonatomic) NSMutableArray *allAttributes;
@end
@implementation ELCVFlowLayout
- (instancetype)init
{
self = [super init];
if (self) {
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
return self;
}
- (void)prepareLayout
{
[super prepareLayout];
_sectionDic = [NSMutableDictionary dictionary];
self.allAttributes = [NSMutableArray array];
//獲取section的數(shù)量
NSUInteger section = [self.collectionView numberOfSections];
for (int sec = 0; sec < section; sec++) {
//獲取每個(gè)section的cell個(gè)數(shù)
NSUInteger count = [self.collectionView numberOfItemsInSection:sec];
for (NSUInteger item = 0; item<count; item++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:sec];
//重新排列
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
[self.allAttributes addObject:attributes];
}
}
}
- (CGSize)collectionViewContentSize
{
//每個(gè)section的頁碼的總數(shù)
NSInteger actualLo = 0;
for (NSString *key in [_sectionDic allKeys]) {
actualLo += [_sectionDic[key] integerValue];
}
return CGSizeMake(actualLo*self.collectionView.frame.size.width, self.collectionView.contentSize.height);
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
{
if(attributes.representedElementKind != nil)
{
return;
}
/*修改by lixinkai 2017.6.30
下面這兩個(gè)方法 itemW种蘸、itemH
解決了同一個(gè)UICollectionView使用不同UICollectionViewCell的問題
*/
//attributes 的寬度
CGFloat itemW = attributes.frame.size.width;
//attributes 的高度
CGFloat itemH = attributes.frame.size.height;
//collectionView 的寬度
CGFloat width = self.collectionView.frame.size.width;
//collectionView 的高度
CGFloat height = self.collectionView.frame.size.height;
//每個(gè)attributes的下標(biāo)值 從0開始
NSInteger itemIndex = attributes.indexPath.item;
CGFloat stride = (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) ? width : height;
//獲取現(xiàn)在的attributes是第幾組
NSInteger section = attributes.indexPath.section;
//獲取每個(gè)section的item的個(gè)數(shù)
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
CGFloat offset = section * stride;
//計(jì)算x方向item個(gè)數(shù)
NSInteger xCount = (width / itemW);
//計(jì)算y方向item個(gè)數(shù)
NSInteger yCount = (height / itemH);
//計(jì)算一頁總個(gè)數(shù)
NSInteger allCount = (xCount * yCount);
//獲取每個(gè)section的頁數(shù)年枕,從0開始
NSInteger page = itemIndex / allCount;
//余數(shù),用來計(jì)算item的x的偏移量
NSInteger remain = (itemIndex % xCount);
//取商贮喧,用來計(jì)算item的y的偏移量
NSInteger merchant = (itemIndex-page*allCount)/xCount;
//x方向每個(gè)item的偏移量
CGFloat xCellOffset = remain * itemW;
//y方向每個(gè)item的偏移量
CGFloat yCellOffset = merchant * itemH;
//獲取每個(gè)section中item占了幾頁
NSInteger pageRe = (itemCount % allCount == 0)? (itemCount / allCount) : (itemCount / allCount) + 1;
//將每個(gè)section與pageRe對(duì)應(yīng)栅盲,計(jì)算下面的位置
[_sectionDic setValue:@(pageRe) forKey:[NSString stringWithFormat:@"%ld", section]];
if(self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
NSInteger actualLo = 0;
//將每個(gè)section中的頁數(shù)相加
for (NSString *key in [_sectionDic allKeys]) {
actualLo += [_sectionDic[key] integerValue];
}
//獲取到的最后的數(shù)減去最后一組的頁碼數(shù)
actualLo -= [_sectionDic[[NSString stringWithFormat:@"%ld", [_sectionDic allKeys].count-1]] integerValue];
xCellOffset += page*width + actualLo*width;
} else {
yCellOffset += offset;
}
attributes.frame = CGRectMake(xCellOffset, yCellOffset, itemW, itemH);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
// UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
UICollectionViewLayoutAttributes *attr = [super layoutAttributesForItemAtIndexPath:indexPath].copy;
[self applyLayoutAttributes:attr];
return attr;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.allAttributes;
}
@end