| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- //
- // MOLeftAlignedFlowLayout.m
- // MiMoLive
- //
- // Created by MiMo on 2025/7/8.
- //
- #import "MOLeftAlignedFlowLayout.h"
- @implementation MOLeftAlignedFlowLayout
- - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
- NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
- NSMutableArray *updatedAttributes = [NSMutableArray array];
-
- // 当前行的 left 起点
- CGFloat leftMargin = self.sectionInset.left;
- CGFloat maxY = -1.0;
- for (UICollectionViewLayoutAttributes *attr in attributes) {
- // 只处理 cell,跳过 header/footer
- if (attr.representedElementCategory == UICollectionElementCategoryCell) {
- // 如果是新的一行
- if (attr.frame.origin.y >= maxY) {
- leftMargin = self.sectionInset.left;
- }
- CGRect frame = attr.frame;
- frame.origin.x = leftMargin;
- attr.frame = frame;
- leftMargin += frame.size.width + self.minimumInteritemSpacing;
- maxY = MAX(CGRectGetMaxY(frame), maxY);
- }
- [updatedAttributes addObject:attr];
- }
-
- return updatedAttributes;
- }
- @end
|