样式
自定义代码
// cell 排版 Typesetting 左 Left 中 Center 右 Right
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, AlignType) {
AlignWithLeft,
AlignWithCenter,
AlignWithRight
};
@interface BaseTypesettingFlowLayout : UICollectionViewFlowLayout
- (instancetype)initWthType:(AlignType)cellType;
/// 全能初始化方法 其他方式初始化最终都会走到这里
- (instancetype)initWithType:(AlignType)cellType betweenOfCell:(CGFloat)betweenOfCell sectionInsetCell:(UIEdgeInsets)sectionInsetCell;
@property (nonatomic, assign) UIEdgeInsets sectionInsetCell;///<
@property (nonatomic, assign) CGFloat betweenOfCell; ///< 两个Cell之间的距离
@property (nonatomic, assign) AlignType cellType; /// cell对齐方式
@end
NS_ASSUME_NONNULL_END
// cell 排版 Typesetting 左 Left 中 Center 右 Right
#import "BaseTypesettingFlowLayout.h"
@interface BaseTypesettingFlowLayout()
@property (nonatomic, assign) CGFloat sumCellWidth; ///< 在居中对齐的时候需要知道这行所有cell的宽度总和
@end
@implementation BaseTypesettingFlowLayout
- (instancetype)init
{
return [self initWithType:AlignWithCenter betweenOfCell:5.0 sectionInsetCell:UIEdgeInsetsMake(5, 5, 5, 5)];
}
- (void)setSectionInsetCell:(UIEdgeInsets)sectionInsetCell
{
_sectionInsetCell = sectionInsetCell;
self.sectionInset = sectionInsetCell;
}
- (void)setBetweenOfCell:(CGFloat)betweenOfCell
{
_betweenOfCell = betweenOfCell;
self.minimumInteritemSpacing = betweenOfCell;
}
- (instancetype)initWthType:(AlignType)cellType
{
return [self initWithType:cellType betweenOfCell:5.0 sectionInsetCell:UIEdgeInsetsMake(5, 5, 5, 5)];
}
- (instancetype)initWithType:(AlignType)cellType betweenOfCell:(CGFloat)betweenOfCell sectionInsetCell:(UIEdgeInsets)sectionInsetCell
{
self = [super init];
if (self) {
self.scrollDirection = UICollectionViewScrollDirectionVertical;
self.minimumLineSpacing = 5;
self.minimumInteritemSpacing = betweenOfCell;
self.sectionInset = sectionInsetCell;//UIEdgeInsetsMake(5, 5, 5, 5);
_betweenOfCell = betweenOfCell;
_cellType = cellType;
}
return self;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *layoutAttributes_t = [super layoutAttributesForElementsInRect:rect];
NSArray *layoutAttributes = [[NSArray alloc] initWithArray:layoutAttributes_t copyItems:YES];
//用来临时存放一行的Cell数组
NSMutableArray *layoutAttributesTemp = [[NSMutableArray alloc]init];
for (NSUInteger index = 0; index < layoutAttributes.count; index++)
{
UICollectionViewLayoutAttributes *currentAttr = layoutAttributes[index]; // 当前cell的位置信息
UICollectionViewLayoutAttributes *previousAttr = index == 0 ? nil : layoutAttributes[index-1]; // 上一个cell 的位置信
UICollectionViewLayoutAttributes *nextAttr = index + 1 == layoutAttributes.count ?
nil : layoutAttributes[index+1];//下一个cell 位置信息
//加入临时数组
[layoutAttributesTemp addObject:currentAttr];
self.sumCellWidth += currentAttr.frame.size.width;
CGFloat previousY = previousAttr == nil ? 0 : CGRectGetMaxY(previousAttr.frame);
CGFloat currentY = CGRectGetMaxY(currentAttr.frame);
CGFloat nextY = nextAttr == nil ? 0 : CGRectGetMaxY(nextAttr.frame);
//如果当前cell是单独一行
if (currentY != previousY && currentY != nextY)
{
if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionHeader])
{
[layoutAttributesTemp removeAllObjects];
self.sumCellWidth = 0.0;
}
else if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionFooter])
{
[layoutAttributesTemp removeAllObjects];
self.sumCellWidth = 0.0;
}
else
{
[self setCellFrameWith:layoutAttributesTemp];
}
}
// 如果下一个cell在本行,这开始调整Frame位置
else if (currentY != nextY)
{
[self setCellFrameWith:layoutAttributesTemp];
}
}
return layoutAttributes;
}
/// 调整属于同一行的cell的位置frame
- (void)setCellFrameWith:(NSMutableArray*)layoutAttributes
{
CGFloat nowWidth = 0.0;
switch (_cellType)
{
case AlignWithLeft:
nowWidth = self.sectionInset.left;
for (UICollectionViewLayoutAttributes *attributes in layoutAttributes)
{
CGRect nowFrame = attributes.frame;
nowFrame.origin.x = nowWidth;
attributes.frame = nowFrame;
nowWidth += nowFrame.size.width + self.betweenOfCell;
}
self.sumCellWidth = 0.0;
[layoutAttributes removeAllObjects];
break;
case AlignWithCenter:
nowWidth = (self.collectionView.frame.size.width - self.sumCellWidth - ((layoutAttributes.count - 1) * _betweenOfCell)) / 2;
for (UICollectionViewLayoutAttributes * attributes in layoutAttributes)
{
CGRect nowFrame = attributes.frame;
nowFrame.origin.x = nowWidth;
attributes.frame = nowFrame;
nowWidth += nowFrame.size.width + self.betweenOfCell;
}
self.sumCellWidth = 0.0;
[layoutAttributes removeAllObjects];
break;
case AlignWithRight:
nowWidth = self.collectionView.frame.size.width - self.sectionInset.right;
for (NSInteger index = layoutAttributes.count - 1 ; index >= 0 ; index-- )
{
UICollectionViewLayoutAttributes * attributes = layoutAttributes[index];
CGRect nowFrame = attributes.frame;
nowFrame.origin.x = nowWidth - nowFrame.size.width;
attributes.frame = nowFrame;
nowWidth = nowWidth - nowFrame.size.width - _betweenOfCell;
}
self.sumCellWidth = 0.0;
[layoutAttributes removeAllObjects];
break;
}
}
@end
拿来使用
BaseTypesettingFlowLayout *flowLayout = [[BaseTypesettingFlowLayout alloc] initWithType:AlignWithRight betweenOfCell:RATIOA(10) sectionInsetCell:UIEdgeInsetsMake(0, 0, 0, RATIOA(15))];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];