封装了一个iOS水平方向瀑布流布局

首先查看效果图
请添加图片描述
是支持多分区的

思路就是和竖直方向上的瀑布流布局是一样的,
只不过这里记录的列是水平方向的,同时要记录下
当前最小的x 所在的列,其实原理和竖直方向上的是相同的
,下面贴出代码

父类layout中的代码

//
//  LBCollectionViewBaseLayout.m
//  TEXT
//
//  Created by mac on 2024/5/12.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "LBCollectionViewBaseLayout.h"
#import "LBCellFakeView.h"
#import "LBCollectionViewLayoutAttributes.h"

typedef NS_ENUM(NSUInteger, LBScrollDirection) {
    LBScrollDirectionStay, //不滚动
    LBScrollDirectionTop, //滚动到顶部
    LBScrollDirectionBottom, //滚动到底部
};

@interface LBCollectionViewBaseLayout () <UIGestureRecognizerDelegate>

//关于拖动的参数
@property (nonatomic, strong) LBCellFakeView *cellFakeView;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
@property (nonatomic, assign) CGPoint fakeCellCenter;
@property (nonatomic, assign) CGPoint panTranslation;
@property (nonatomic, assign) LBScrollDirection continousScrollDirection;
@property (nonatomic, strong) CADisplayLink *displayLink;

@end

@implementation LBCollectionViewBaseLayout

{
    BOOL _isNeedReCalculateAllLayout;
}

- (instancetype)init
{
    if (self = [super init]) {
        self.isFloor = YES;
        self.canDrag = NO;
        self.header_suppension = NO;
        self.layoutType = LBLayoutTypeTypeFillLayout;
        self.columnCount = 1;
        self.columnSortType = LBColumnSortTypeMinHeight;
        self.fixTop = 0;
        self.xBeyong = 3;
        _isNeedReCalculateAllLayout = YES;
        _headerAttributesArray = [NSMutableArray array];
        [self addObserver:self forKeyPath:@"collectionView" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

#pragma mark -  当尺寸有所变化时, 重新刷新

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return self.header_suppension;
}

- (void)invalidateLayoutWithContext:(UICollectionViewLayoutInvalidationContext *)context
{
    /***
     外部掉用reloadData 或者变更任意数据时则认为需要进行全量布局的刷新
     好处时候在外部变量变更数据时内部布局会及时刷新
     劣势是在你上拉加载某一页时,布局会全部整体重新计算一遍,并非只计算新增的布局
     */
    _isNeedReCalculateAllLayout = context.invalidateEverything ||
    context.invalidateDataSourceCounts;
    [super invalidateLayoutWithContext:context];
}

//注册所有的背景view(传入类名)
- (void)registerDecorationView:(NSArray<NSString *> *)classNames
{
    for (NSString *className in classNames) {
        if (className.length > 0) {
            [self registerClass:NSClassFromString(className) forDecorationViewOfKind:className];
        }
    }
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"collectionView"];
}

#pragma mark - 所有cell和view 的布局属性
- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    if (!self.attributesArray ||
        self.collectionView.numberOfSections == 0) {
        return [super layoutAttributesForElementsInRect:rect];
    }
    if (self.header_suppension) {
        //只有在headerAttributesArray 里面查找需要悬浮的属性
        for (UICollectionViewLayoutAttributes *attribute in self.headerAttributesArray) {
            if (![attribute.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
                continue;
            }
            NSInteger section = attribute.indexPath.section;
            CGRect frame = attribute.frame;
            BOOL isNeedChangeFrame = NO;
            if (section == 0) {
                if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
                    CGFloat offsetY = self.collectionView.contentOffset.y + self.fixTop;
                    if (offsetY > 0 && offsetY < [self.collectionHeightsArray[0] floatValue]) {
                        frame.origin.y = offsetY;
                        attribute.zIndex = 1000 + section;
                        attribute.frame = frame;
                        isNeedChangeFrame = YES;
                    }
                } else {
                    CGFloat offsetX = self.collectionView.contentOffset.x + self.fixTop;
                    if (offsetX > 0 && offsetX < [self.collectionHeightsArray[0] floatValue]) {
                        frame.origin.x = offsetX;
                        attribute.zIndex = 1000 + section;
                        attribute.frame = frame;
                        isNeedChangeFrame = YES;
                    }
                }
            } else {
                if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
                    CGFloat offsetY = self.collectionView.contentOffset.y + self.fixTop;
                    if (offsetY > [self.collectionHeightsArray[section - 1] floatValue] &&
                        offsetY < [self.collectionHeightsArray[section] floatValue]) {
                        frame.origin.y = offsetY;
                        attribute.zIndex = 1000 + section;
                        attribute.frame = frame;
                        isNeedChangeFrame = YES;
                    }
                } else {
                    CGFloat offsetX = self.collectionView.contentOffset.x + self.fixTop;
                    if (offsetX > [self.collectionHeightsArray[section - 1] floatValue] &&
                        offsetX < [self.collectionHeightsArray[section] floatValue]) {
                        frame.origin.x = offsetX;
                        attribute.zIndex = 1000 + section;
                        attribute.frame = frame;
                        isNeedChangeFrame = YES;
                    }
                }
            }
            if (isNeedChangeFrame) {
                /**
                 这里需要注意,在悬浮情况下,改变了headeatt的frame,
                 在滑出header又滑回来时,headeAttr已经被修改过,需要改回原始值,
                 否则header无法正确回归
                 */
                if ([attribute isKindOfClass:[LBCollectionViewLayoutAttributes class]]) {
                    attribute.frame = ((LBCollectionViewLayoutAttributes *)attribute).originalFrame;
                }
            }
        }
    }
    return self.attributesArray;
}

#pragma mark - 以下是拖动排序代码

- (void)setCanDrag:(BOOL)canDrag
{
    _canDrag = canDrag;
    if (canDrag) {
        if (self.longPress == nil && self.panGesture == nil) {
            [self setUpGestureRecognizers];
        }
    } else {
        [self.collectionView removeGestureRecognizer:self.longPress];
        self.longPress.delegate = nil;
        self.longPress = nil;
        [self.collectionView removeGestureRecognizer:self.panGesture];
        self.panGesture.delegate = self;
        self.panGesture = nil;
    }
}

#pragma mark - observe

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"collectionView"]) {
        if (self.canDrag) {
            [self setUpGestureRecognizers];
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)setUpGestureRecognizers
{
    if (self.collectionView == nil) {
        return;
    }
    self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    self.longPress.delegate = self;
    self.panGesture.delegate = self;
    self.panGesture.maximumNumberOfTouches = 1;
    NSArray *gestures = [self.collectionView gestureRecognizers];
    __weak typeof(LBCollectionViewBaseLayout *) weakSelf = self;
    [gestures enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UILongPressGestureRecognizer class]]) {
            [(UILongPressGestureRecognizer *)obj requireGestureRecognizerToFail:weakSelf.longPress];
        }
    }];
    [self.collectionView addGestureRecognizer:self.longPress];
    [self.collectionView addGestureRecognizer:self.panGesture];
}

#pragma mark - gesture

- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
    CGPoint location = [longPress locationInView:self.collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location];
    if (_cellFakeView != nil) {
        indexPath = self.cellFakeView.indexPath;
    }
    
    if (indexPath != nil) {
        return;
    }
    
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
        {
            self.collectionView.scrollsToTop = NO;
            UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
            
            self.cellFakeView = [[LBCellFakeView alloc] initWithCell:cell];
            self.cellFakeView.indexPath = indexPath;
            self.cellFakeView.originCenter = cell.center;
            self.cellFakeView.cellFrame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
            
            [self.collectionView addSubview:self.cellFakeView];
            self.fakeCellCenter = self.cellFakeView.center;
            [self invalidateLayout];
            [self.cellFakeView pushForwardView];
        }
            break;
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateEnded:
        {
            [self cancelDrag:indexPath];
        }
            
        default:
            break;
    }
}

//pan gesture

- (void)handlePanGesture:(UIPanGestureRecognizer *)pan
{
    _panTranslation = [pan translationInView:self.collectionView];
    if (_cellFakeView != nil) {
        switch (pan.state) {
            case UIGestureRecognizerStateChanged:
                {
                    CGPoint center = _cellFakeView.center;
                    center.x = self.fakeCellCenter.x + self.panTranslation.x;
                    center.y = self.fakeCellCenter.y + self.panTranslation.y;
                    self.cellFakeView.center = center;
                    [self beginScrollIfNeeded];
                    [self moveItemIfNeeded];
                }
                break;
            case UIGestureRecognizerStateCancelled:
            case UIGestureRecognizerStateEnded:
            {
                [self invalidateDisplayLink];
            }
                break;
            default:
                break;
        }
    }
}

#pragma mark - gesturedelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint location = [gestureRecognizer locationInView:self.collectionView];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location];
    if (!indexPath) {
        return NO;
    }
    
    if ([gestureRecognizer isEqual:self.longPress]) {
        return (self.collectionView.panGestureRecognizer.state == UIGestureRecognizerStatePossible ||
                self.collectionView.panGestureRecognizer.state == UIGestureRecognizerStateFailed);
    } else if ([gestureRecognizer isEqual:self.panGesture]) {
        return (self.longPress.state != UIGestureRecognizerStatePossible &&
                self.longPress.state != UIGestureRecognizerStateFailed);
    }
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([self.panGesture isEqual:gestureRecognizer]) {
        return [self.longPress isEqual:otherGestureRecognizer];
    } else if ([self.collectionView.panGestureRecognizer isEqual:gestureRecognizer]) {
        return (self.longPress.state != UIGestureRecognizerStatePossible &&
                self.longPress.state != UIGestureRecognizerStateFailed);
    }
    return YES;
}

- (void)cancelDrag:(NSIndexPath *)indexPath
{
    if (self.cellFakeView == nil) {
        return;
    }
    
    self.collectionView.scrollsToTop = YES;
    self.fakeCellCenter = CGPointZero;
    [self invalidateDisplayLink];
    __weak typeof (LBCollectionViewBaseLayout *) weakSelf = self;
    [self.cellFakeView pushBackView:^{
        [weakSelf.cellFakeView removeFromSuperview];
        weakSelf.cellFakeView = nil;
        [weakSelf invalidateLayout];
    }];
}

- (void)moveItemIfNeeded
{
    NSIndexPath *atIndexPath = nil;
    NSIndexPath *toIndexPath = nil;
    __weak typeof (LBCollectionViewBaseLayout *) weakSelf = self;
    if (self.cellFakeView) {
        atIndexPath = self.cellFakeView.indexPath;
        toIndexPath = [self.collectionView indexPathForItemAtPoint:self.cellFakeView.center];
    }
    if (atIndexPath.section != toIndexPath.section) {
        return;
    }
    if (atIndexPath == nil || toIndexPath == nil) {
        return;
    }
    
    if ([atIndexPath isEqual:toIndexPath]) {
        return;
    }
    
    UICollectionViewLayoutAttributes *attribute = nil;
    
    for (LBCollectionViewLayoutAttributes *attr in weakSelf.attributesArray) {
        if (attr.indexPath.section == toIndexPath.section && attr.indexPath.item == toIndexPath.item
            && ![attr.representedElementKind isEqualToString: UICollectionElementKindSectionHeader]
            && ![attr.representedElementKind isEqualToString: UICollectionElementKindSectionFooter]) {
            attribute = attr;
            break;;
        }
    }
    
    if (attribute != nil) {
        [self.collectionView performBatchUpdates:^{
            weakSelf.cellFakeView.indexPath = toIndexPath;
            weakSelf.cellFakeView.cellFrame = attribute.frame;
            [weakSelf.cellFakeView changeBoundsIfNeeded:attribute.bounds];
            [weakSelf.collectionView moveItemAtIndexPath:atIndexPath toIndexPath:toIndexPath];
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
                [weakSelf.delegate collectionview:weakSelf.collectionView layout:weakSelf didMoveCell:atIndexPath toIndexPath:toIndexPath];
            }
        } completion:nil];
    }
}

- (void)setUpDisPlayLink
{
    if (_displayLink) {
        return;
    }
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(continuousScroll)];
    [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)invalidateDisplayLink
{
    _continousScrollDirection = LBScrollDirectionStay;
    [_displayLink invalidate];
    _displayLink = nil;
}

- (void)continuousScroll
{
    if(_cellFakeView == nil) {
        return;
    }
    CGFloat percentage = [self calcTriggerPercentage];
    CGFloat scrollRate = [self scrollValueWithSpeed:10 andPercentage:percentage];
    
    CGFloat offset = 0;
    CGFloat insetTop = 0;
    CGFloat insetEnd = 0;
    CGFloat length = self.scrollDirection == UICollectionViewScrollDirectionVertical ? self.collectionView.frame.size.height : self.collectionView.frame.size.width;
    
    CGFloat contentLength = self.scrollDirection == UICollectionViewScrollDirectionVertical ?
    self.collectionView.contentSize.height : self.collectionView.contentSize.width;
    
    if (contentLength + insetTop + insetEnd <= length) {
        return;
    }
    
    if (offset + scrollRate <=  - insetTop) {
        scrollRate = - insetEnd - offset;
    } else if (offset + scrollRate >= contentLength + insetEnd - length) {
        scrollRate = contentLength + insetEnd - length - offset;
    }
    
    __weak typeof (LBCollectionViewBaseLayout *) weakSelf = self;
    [self.collectionView performBatchUpdates:^{
        if (weakSelf.scrollDirection == UICollectionViewScrollDirectionVertical) {
            CGPoint point = weakSelf.fakeCellCenter;
            point.y += scrollRate;
            weakSelf.fakeCellCenter = point;
            CGPoint center = weakSelf.cellFakeView.center;
            center.y = weakSelf.fakeCellCenter.y + weakSelf.panTranslation.y;
            weakSelf.cellFakeView.center = center;
            CGPoint contentOffset = weakSelf.collectionView.contentOffset;
            contentOffset.y += scrollRate;
            weakSelf.collectionView.contentOffset = contentOffset;
        } else {
            CGPoint point = weakSelf.fakeCellCenter;
            point.x += scrollRate;
            weakSelf.fakeCellCenter = point;
            CGPoint center = weakSelf.cellFakeView.center;
            center.x = weakSelf.fakeCellCenter.x + weakSelf.panTranslation.x;
            weakSelf.cellFakeView.center = center;
            CGPoint contentOffset = weakSelf.collectionView.contentOffset;
            contentOffset.x += scrollRate;
            weakSelf.collectionView.contentOffset = contentOffset;
        }
    } completion:^(BOOL finished) {
        
    }];

}

- (CGFloat)calcTriggerPercentage
{
    if (_cellFakeView == nil) {
        return 0;
    }
    
    CGFloat offset = 0;
    CGFloat offsetEnd = 0 + self.scrollDirection == UICollectionViewScrollDirectionVertical ? self.collectionView.frame.size.height : self.collectionView.frame.size.width;
    CGFloat insetTop = 0;
    CGFloat triggerInsetTop = 0;
    CGFloat triggerInsetEnd = 0;
    CGFloat paddingTop = 0;
    CGFloat paddingEnd = 0;
    CGFloat percentage = 0;
    if (self.continousScrollDirection == LBScrollDirectionTop) {
        if (self.cellFakeView) {
            percentage = 1 - (((self.scrollDirection == UICollectionViewScrollDirectionVertical ? self.cellFakeView.frame.origin.y : self.cellFakeView.frame.origin.x) - (offset + paddingTop)) / triggerInsetTop);
        }
    } else if (self.continousScrollDirection == LBScrollDirectionBottom) {
        if (self.cellFakeView) {
            percentage = 1.0 - (((insetTop + offsetEnd - paddingEnd) - ((self.scrollDirection == UICollectionViewScrollDirectionVertical ? self.cellFakeView.frame.origin.y : self.cellFakeView.frame.origin.x) + (self.scrollDirection == UICollectionViewScrollDirectionVertical ? self.cellFakeView.frame.size.height : self.cellFakeView.frame.size.width) + insetTop)) / triggerInsetEnd);
        }
    }
    percentage = fmin(1.0f, percentage);
    percentage = fmax(0, percentage);
    return percentage;
}

- (void)beginScrollIfNeeded
{
    if (self.cellFakeView == nil) {
        return;
    }
    CGFloat offset = self.scrollDirection == UICollectionViewScrollDirectionVertical ? self.collectionView.contentOffset.y : self.collectionView.contentOffset.x;
    CGFloat triggerInsetTop = self.scrollDirection == UICollectionViewScrollDirectionVertical ?
    self.collectionView.contentInset.top : self.collectionView.contentInset.left;
    CGFloat triggerInsetEnd = self.scrollDirection == UICollectionViewScrollDirectionVertical ?
    self.collectionView.contentInset.bottom : self.collectionView.contentInset.right;
    CGFloat paddingTop = 0;
    CGFloat paddingend = 0;
    CGFloat length = self.scrollDirection == UICollectionViewScrollDirectionVertical ? self.collectionView.frame.size.height : self.collectionView.frame.size.width;
    CGFloat fakeCellTopEdge = self.scrollDirection == UICollectionViewScrollDirectionVertical ? CGRectGetMinY(self.cellFakeView.frame) : CGRectGetMinX(self.cellFakeView.frame);
    CGFloat fakeCellEndEdge = self.scrollDirection == UICollectionViewScrollDirectionVertical ? CGRectGetMaxY(self.cellFakeView.frame) : CGRectGetMaxX(self.cellFakeView.frame);
    if (fakeCellTopEdge <= offset + paddingTop + triggerInsetTop) {
        self.continousScrollDirection = LBScrollDirectionTop;
        [self setUpDisPlayLink];
    } else if (fakeCellEndEdge >= offset + length - paddingend - triggerInsetEnd) {
        self.continousScrollDirection = LBScrollDirectionBottom;
        [self setUpDisPlayLink];
    } else {
        [self invalidateDisplayLink];
    }
}

#pragma mark - getter

- (CGFloat)scrollValueWithSpeed:(CGFloat)speed 
                  andPercentage:(CGFloat)percentage
{
    CGFloat value = 0.0f;
    switch (self.continousScrollDirection) {
        case LBScrollDirectionStay:
            return 0;
            break;
        case LBScrollDirectionTop:
            value = -speed;
            break;
        case LBScrollDirectionBottom:
            value = speed;
            break;
        default:
            return 0;
            break;
    }
    CGFloat proofedPercentage = fmax(fmin(1, percentage), 0);
    return value * proofedPercentage;
}

- (void)forceSetIsNeedReCaculateAllLayout:(BOOL)isNeedReCaculateAllLayout
{
    _isNeedReCalculateAllLayout = isNeedReCaculateAllLayout;
}

@end

子类layout 中的代码

//
//  LBHorizontalLayout.m
//  TEXT
//
//  Created by mac on 2024/5/18.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "LBHorizontalLayout.h"
#import "LBCollectionReusableView.h"
#import "LBCollectionViewLayoutAttributes.h"
#import "LBCollectionViewBackgroundViewLayoutAttributes.h"

@implementation LBHorizontalLayout

#pragma mark - 初始化属性

- (instancetype)init
{
    if (self = [super init]) {
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return self;
}

- (void)prepareLayout
{
    [super prepareLayout];
    
    if (!self.isNeedReCalculateAllLayout) {
        //不需要重新计算
        return;
    }
    
    CGFloat totalHeight = self.collectionView.frame.size.height;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat headerW = 0;
    CGFloat footerW = 0;
    UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
    CGFloat minimumLineSpacing = 0;
    CGFloat minimumInterItemSpacing = 0;
    NSInteger sectionCount = [self.collectionView numberOfSections];
    self.attributesArray = [NSMutableArray array];
    self.collectionHeightsArray = [NSMutableArray array];
    for (int index = 0; index < sectionCount; index ++) {
        NSInteger itemCount = [self.collectionView numberOfItemsInSection:index];
        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)]) {
            headerW = [self.delegate collectionView:self.collectionView layout:self referenceSizeForHeaderInSection:index].width;
        } else {
            headerW = self.headerReferenceSize.width;
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)]) {
            footerW = [self.delegate collectionView:self.collectionView layout:self referenceSizeForFooterInSection:index].width;
        } else {
            footerW = self.footerReferenceSize.width;
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
            edgeInsets = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:index];
        } else {
            edgeInsets = self.sectionInset;
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:minimumLineSpacingForSectionAtIndex:)]) {
            minimumLineSpacing = [self.delegate collectionView:self.collectionView layout:self minimumLineSpacingForSectionAtIndex:index];
        } else {
            minimumLineSpacing = self.minimumLineSpacing;
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
            minimumInterItemSpacing = [self.delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:index];
        } else {
            minimumInterItemSpacing = self.minimumInteritemSpacing;
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:registerBackView:)]) {
            NSString *className = [self.delegate collectionView:self.collectionView layout:self registerBackView:index];
            if (className != nil && className.length > 0) {
                NSAssert([[NSClassFromString(className) alloc] init] != nil, @"代理collectionView:layout:registerBackView:里面必须返回有效的类名");
                [self registerClass:NSClassFromString(className) forDecorationViewOfKind:className];
            } else {
                [self registerClass:[LBCollectionReusableView class] forDecorationViewOfKind:@"LBCollectionReusableView"];
            }
        } else {
            [self registerClass:[LBCollectionReusableView class] forDecorationViewOfKind:@"LBCollectionReusableView"];
        }
        x = [self maxHeightWithSection:index];
        y = edgeInsets.top;
        
        if (headerW > 0) {
            NSIndexPath *headerIndexPath = [NSIndexPath indexPathForItem:0 inSection:index];
            LBCollectionViewLayoutAttributes *headerAttr = [LBCollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:headerIndexPath];
            /*注意,因为这里是水平布局,所以self.collectionView.frame.size.height 是一个固定的很小的
             值
             */
            headerAttr.frame = CGRectMake(x, 0, headerW, self.collectionView.frame.size.height);
            [headerAttr setValue:[NSValue valueWithCGRect:headerAttr.frame] forKey:@"orginalFrame"];
            [self.attributesArray addObject:headerAttr];
            [self.headerAttributesArray addObject:headerAttr];
        }
        x += headerW;
        CGFloat itemStartX = x;
        CGFloat lastX = x;
        if (itemCount > 0) {
            x += edgeInsets.left;
            if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:typeOfLayout:)]) {
                self.layoutType = [self.delegate collectionView:self.collectionView layout:self typeOfLayout:index];
            }
            NSAssert((self.layoutType == LBLayoutTypeLabelVerticalLayout ||
                      self.layoutType == LBLayoutTypeColumnLayout ||
                      self.layoutType == LBLayoutTypeAbsoluteLayout), @"横向布局暂时只支持 LBLayoutTypeLabelVerticalLayout, LBLayoutTypeColumnLayout, LBLayoutTypeAbsoluteLayout");
            
            //NSInteger columnCount = 1;
            if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:columnCountOfSection:)]) {
                self.columnCount = [self.delegate collectionView:self.collectionView layout:self columnCountOfSection:index];
            }
            
            //定义一个列高数组,记录每一列的总高度
            CGFloat *columnWidths = (CGFloat *)malloc(self.columnCount * sizeof(CGFloat));
            //cell的高度
            CGFloat itemHeight = 0.0;
            if (self.layoutType == LBLayoutTypeColumnLayout) {
                for (int i = 0; i < self.columnCount; i ++) {
                    if (i == 0 && self.topLeftGap > 0) {
                        columnWidths[i] = self.topLeftGap + x;
                    } else if (i == 1 && self.bottomLeftGap > 0) {
                        columnWidths[i] = self.bottomLeftGap;
                    } else {
                        columnWidths[i] = x;
                    }
                }
                itemHeight = (totalHeight - edgeInsets.top - edgeInsets.bottom - minimumLineSpacing * (self.columnCount - 1)) / self.columnCount;
            }
            NSInteger lastColumnIndex = 0;
            NSMutableArray *arrayOfAbsolute = [NSMutableArray array]; //储存绝对定位布局的额数组
            
            for (int i = 0; i < itemCount; i ++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:index];
                CGSize itemSize = CGSizeZero;
                if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:)]) {
                    itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
                } else {
                    itemSize = self.itemSize;
                }
                
                LBCollectionViewLayoutAttributes *attributes = [LBCollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
                
                NSInteger preRow = self.attributesArray.count - 1;
                switch (self.layoutType) {
#pragma mark - 纵向标签布局处理
                    case LBLayoutTypeLabelVerticalLayout:
                    {
                        if (preRow >= 0) {
                            if (i > 0) {
                                LBCollectionViewLayoutAttributes *preAttr = self.attributesArray[preRow];
                                y = preAttr.frame.origin.y + preAttr.frame.size.height + minimumInterItemSpacing;
                                if (y + itemSize.height > totalHeight - edgeInsets.bottom) {
                                    y = edgeInsets.top;
                                    x += itemSize.width + minimumLineSpacing;
                                }
                            }
                        }
                        attributes.frame = CGRectMake(x, y, itemSize.width, itemSize.height);
                    }
                        break;
                    case LBLayoutTypeLabelHorizontalLayout: {
                        
                    }
                        break;
#pragma mark - 列布局处理 |横向标签布局处理
                    case LBLayoutTypeColumnLayout:
                    {
                        CGFloat max = CGFLOAT_MAX;
                        NSInteger column = 0;
                        if (self.columnSortType == LBColumnSortTypeSequence) {
                            column = lastColumnIndex;
                        } else {
                            for (int i = 0; i < self.columnCount; i ++) {
                                if (columnWidths[i] < max) {
                                    max = columnWidths[i];
                                    column = i;
                                }
                            }
                        }
                        CGFloat itemX = columnWidths[column];
                        CGFloat itemY = edgeInsets.top + (itemHeight + minimumInterItemSpacing) * column;
                        if (self.manulHeight) {
                            attributes.frame = CGRectMake(itemX, itemY, itemSize.width, itemSize.height);
                        } else {
                            attributes.frame = CGRectMake(itemX, itemY, itemSize.width, itemHeight);
                        }
                        NSLog(@"哈哈哈这里的布局%@", NSStringFromCGRect(attributes.frame));
                        if (self.manulHeight) {
                            if (itemSize.height > itemHeight) {
                                CGFloat delta = itemSize.height - itemHeight;
                                if (delta > minimumInterItemSpacing) {
                                    NSInteger k = ceil(delta/(itemHeight + minimumInterItemSpacing));
                                    for (int i = 0; i < k ; i ++) {
                                        if ((column + i + 1) < self.columnCount) {
                                            columnWidths[column + i + 1] += (itemSize.width + minimumLineSpacing);
                                        }
                                    }
                                }
                            }
                        }
                        columnWidths[column] += (itemSize.width + minimumLineSpacing);
                        lastColumnIndex ++;
                        if (lastColumnIndex >= self.columnCount) {
                            lastColumnIndex = 0;
                        }
                    }
                        break;
                    case LBLayoutTypeAbsoluteLayout:
                    {
                        CGRect itemFrame = CGRectZero;
                        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:rectOfItem:)]) {
                            itemFrame = [self.delegate collectionView:self.collectionView layout:self rectOfItem:indexPath];
                        }
                        CGFloat absolute_x = x + itemFrame.origin.x;
                        CGFloat absolute_y = edgeInsets.top + itemFrame.origin.y;
                        CGFloat absolute_h = itemFrame.size.height;
                        
                        if ((absolute_y + absolute_h > self.collectionView.frame.size.height - edgeInsets.bottom) &&
                            (absolute_y < self.collectionView.frame.size
                             .height - edgeInsets.top)) {
                                 absolute_h -= (absolute_y + absolute_h - (self.collectionView.frame.size.height - edgeInsets.bottom));
                             }
                        
                        CGFloat absolute_w = itemFrame.size.width;
                        attributes.frame = CGRectMake(absolute_x, absolute_y, absolute_w, absolute_h);
                        [arrayOfAbsolute addObject:attributes];
                    }
                        break;
                        
                    default:
                        break;
                }
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:transformOfItem:)]) {
                    attributes.transform3D = [self.delegate collectionView:self.collectionView layout:self transformOfItem:indexPath];
                }
                if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:zIndexOfItem:)]) {
                    attributes.zIndex = [self.delegate collectionView:self.collectionView layout:self zIndexOfItem:indexPath];
                }
                if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:alphaOfItem:)]) {
                    attributes.alpha = [self.delegate collectionView:self.collectionView layout:self alphaOfItem:indexPath];
                }
                attributes.indexPath = indexPath;
                if (self.layoutType != LBLayoutTypePercentLayout) {
                    [self.attributesArray addObject:attributes];
                }
                
                if(self.layoutType == LBLayoutTypeColumnLayout) {
                    CGFloat max = 0;
                    for (int i = 0; i < self.columnCount; i ++) {
                        if (columnWidths[i] > max) {
                            max = columnWidths[i];
                        }
                    }
                    lastX = max;
                } else if (self.layoutType == LBLayoutTypeAbsoluteLayout) {
                    if (i == itemCount - 1) {
                        for (LBCollectionViewLayoutAttributes *attr in arrayOfAbsolute) {
                            if (lastX < attr.frame.origin.x + attr.frame.size.width) {
                                lastX = attr.frame.origin.x + attr.frame.size.width;
                            }
                        }
                    }
                } else {
                    lastX = attributes.frame.origin.x + attributes.frame.size.width;
                }
            }
            free(columnWidths);
        }
        
        if (self.layoutType == LBLayoutTypeColumnLayout) {
            if (itemCount > 0) {
                lastX -= minimumLineSpacing;
            }
        }
        
        if (itemCount > 0) {
            lastX += edgeInsets.right;
        }
        
        //添加页脚属性
        if (footerW > 0) {
            NSIndexPath *footerIndexPath = [NSIndexPath indexPathForItem:0 inSection:index];
            LBCollectionViewLayoutAttributes *footerAttr = [LBCollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:footerIndexPath];
            footerAttr.frame = CGRectMake(lastX, 0, footerW, self.collectionView.frame.size.height);
            [self.attributesArray addObject:footerAttr];
            lastX += footerW;
        }
        
        //添加背景视图
        CGFloat backWidth = lastX - itemStartX + ([self isAttachToBottom:index] ? headerW : 0) - ([self isAttachToTop:index] ? 0: footerW);
        if (backWidth < 0) {
            backWidth = 0;
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:registerBackView:)]) {
            NSString *className = [self.delegate collectionView:self.collectionView layout:self registerBackView:index];
            if (className != nil && className.length > 0) {
                LBCollectionViewBackgroundViewLayoutAttributes *attr = [LBCollectionViewBackgroundViewLayoutAttributes layoutAttributesForDecorationViewOfKind:className withIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
                attr.frame = CGRectMake([self isAttachToTop:index] ? itemStartX - headerW: itemStartX, 0, backWidth, self.collectionView.frame.size.height);
                attr.zIndex = - 1000;
                if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backgroundViewMethodForSection:)]) {
                    if ([self.delegate collectionView:self.collectionView layout:self backgroundViewMethodForSection:index] != nil) {
                        [attr callMethod:[self.delegate collectionView:self.collectionView layout:self backgroundViewMethodForSection:index]];
                    }
                }
                [self.attributesArray addObject:attr];
            } else {
                LBCollectionViewLayoutAttributes *attr = [LBCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"LBCollectionReusableView" withIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
                attr.frame = CGRectMake([self isAttachToTop:index] ? itemStartX - headerW : itemStartX, 0, backWidth, self.collectionView.frame.size.height);
                attr.color = self.collectionView.backgroundColor;
                if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backColorForSection:)]) {
                    attr.color = [self.delegate collectionView:self.collectionView layout:self backColorForSection:index];
                }
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backImageForSection:)]) {
                    attr.image = [self.delegate collectionView:self.collectionView layout:self backImageForSection:index];
                }
                attr.zIndex = - 1000;
                [self.attributesArray addObject:attr];
            }
        } else {
            LBCollectionViewLayoutAttributes *attr = [LBCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:@"LBCollectionReusableView" withIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]];
            attr.frame = CGRectMake([self isAttachToTop:index] ? itemStartX - headerW : itemStartX, 0, backWidth, self.collectionView.frame.size.height);
            attr.color = self.collectionView.backgroundColor;
            if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backColorForSection:)]) {
                attr.color = [self.delegate collectionView:self.collectionView layout:self backColorForSection:index];
            }
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:backImageForSection:)]) {
                attr.image = [self.delegate collectionView:self.collectionView layout:self backImageForSection:index];
            }
            attr.zIndex = - 1000;
            [self.attributesArray addObject:attr];
        }
        self.collectionHeightsArray[index] = [NSNumber numberWithFloat:lastX];
    }
    [self forceSetIsNeedReCaculateAllLayout:NO];
}

#pragma mark - 内容size

- (CGSize)collectionViewContentSize
{
    if (self.collectionHeightsArray.count < 0) {
        return CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
    }
    
    CGFloat footerW = 0;
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)]) {
        footerW = [self.delegate collectionView:self.collectionView layout:self referenceSizeForFooterInSection:self.collectionHeightsArray.count - 1].width;
    } else {
        footerW = self.footerReferenceSize.width;
    }
    
    UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]) {
        edgeInsets = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:self.collectionHeightsArray.count - 1];
    } else {
        edgeInsets = self.sectionInset;
    }
    return CGSizeMake([self.collectionHeightsArray[self.collectionHeightsArray.count - 1] floatValue], self.collectionView.frame.size.height);
}

- (BOOL)isAttachToTop:(NSInteger)section
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:attachToTop:)]) {
        return [self.delegate collectionView:self.collectionView layout:self attachToTop:section];
    }
    return NO;
}

- (BOOL)isAttachToBottom:(NSInteger)section
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView:layout:attachToBottom:)]) {
        return [self.delegate collectionView:self.collectionView layout:self attachToBottom:section];
    }
    return NO;
}
/// 每个区的初始x坐标
/// - Parameter section: 区索引
- (CGFloat)maxHeightWithSection:(NSInteger)section
{
    if (section > 0) {
        return [self.collectionHeightsArray[section - 1] floatValue];
    } else {
        return 0;
    }
}

@end

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/644282.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

08-Fortran基础--Fortran内置函数分类总结

08-Fortran基础--Fortran内置函数分类总结 0 引言1 Fortran内置函数1.1 常用到数学函数1.2 字符串函数&#xff1a;1.3 数组函数&#xff1a;1.4 数值查询函数1.5 文件操作函数&#xff1a; 2 结语 0 引言 Fortran是一种很古老的编程语言&#xff0c;但它仍然广泛使用于科学计算…

使用LoRA进行高效微调:基本原理

Using LoRA for efficient fine-tuning: Fundamental principles — ROCm Blogs (amd.com) [2106.09685] LoRA: Low-Rank Adaptation of Large Language Models (arxiv.org) Parametrizations Tutorial — PyTorch Tutorials 2.3.0cu121 documentation 大型语言模型&#xf…

基于地理坐标的高阶几何编辑工具算法(7)——矩形绘制

文章目录 工具步骤应用场景示意图算法原理工具步骤 点击矩形绘制工具,点击三个点完成矩形绘制。 应用场景 用于在地图上快速绘制任意方向的矩形。 示意图 算法原理 点第一个点确定矩形的一个角点P1,也作为平移后的坐标原点,生成平移矩阵。点第二个点P2,确定矩形的一条边…

Studio 3T 2024.3 (macOS, Linux, Windows) - MongoDB 的专业 GUI、IDE 和 客户端,支持自然语言查询

Studio 3T 2024.3 (macOS, Linux, Windows) - MongoDB 的专业 GUI、IDE 和 客户端&#xff0c;支持自然语言查询 The professional GUI, IDE and client for MongoDB 请访问原文链接&#xff1a;https://sysin.org/blog/studio-3t/&#xff0c;查看最新版。原创作品&#xff…

CentOS 7.9 邮箱部署——Postfix+Dovecot详细

PostfixDovecot 文章目录 PostfixDovecot资源列表基础环境一、部署DNS二、部署postfix和dovecot2.1、配置postfix2.2、配置dovecot2.3、创建邮件用户 三、发送邮件测试3.1、windows安装poxmail3.2、登录邮箱3.3、发送接收邮件 四、搭建SSL认证加密4.1、生成私钥4.2、生成公钥4.…

状态转换图

根据本章开头讲的结构化分析的第3条准则,在需求分析过程中应该建立起软件系统的行为模型。状态转换图(简称为状态图)通过描绘系统的状态及引起系统状态转换的事件,来表示系统的行为。此外,状态图还指明了作为特定事件的结果系统将做哪些动作(例如,处理数据)。因此,状态图提供了…

iMX6ULL 嵌入式linux开发 | 4G无线广播终端实现方案介绍

现有的有线广播&#xff0c;如村上的大喇叭&#xff0c;需要布线&#xff0c;施工麻烦。借助现有的4G网络&#xff0c;传输音频流完全没问题&#xff0c;4G网络结合流媒体技术和MQTT消息传递实现设备间的同步推拉流。这种方案可以避免有线布线的麻烦&#xff0c;同时实现4G无线…

基于springboot+vue的智慧外贸平台

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

Elasticsearch 分析器(内置分析器,自定义分析器,IK分析器)

Elasticsearch 分析器&#xff08;内置分析器&#xff0c;自定义分析器&#xff0c;IK分析器&#xff09; 内置分析器使用分析器自定义分析器中文分析器&#xff08;IK分析器&#xff09;安装使用添加词典 内置分析器 官网&#xff1a;https://www.elastic.co/guide/en/elasti…

如何确保大模型 RAG 生成的信息是基于可靠的数据源?

在不断发展的人工智能 (AI) 领域中&#xff0c;检索增强生成 (RAG) 已成为一种强大的技术。 RAG 弥合了大型语言模型 (LLM) 与外部知识源之间的差距&#xff0c;使 AI 系统能够提供更全面和信息丰富的响应。然而&#xff0c;一个关键因素有时会缺失——透明性。 我们如何能够…

翻译《The Old New Thing》- What‘s the deal with the EM_SETHILITE message?

Whats the deal with the EM_SETHILITE message? - The Old New Thing (microsoft.com)https://devblogs.microsoft.com/oldnewthing/20071025-00/?p24693 Raymond Chen 2007年10月25日 简要 文章讨论了EM_SETHILITE和EM_GETHILITE消息在文档中显示为“未实现”的原因。这些…

Redis开发实战

单机部署安装 服务端下载&#xff0c;安装&#xff0c;启动去官网下载最新的版本&#xff1a;http://redis.io/download &#xff0c;这里用的是3.0.2解压后&#xff0c;进入解压好的文件夹redis的安装非常简单&#xff0c;因为已经有现成的Makefile文件&#xff0c;所以直接先…

NASA数据集——阿尔法喷气式大气实验甲醛(HCHO)数据

Alpha Jet Atmospheric eXperiment Formaldehyde Data 简介 阿尔法喷气式大气实验甲醛数据 阿尔法喷气式大气实验&#xff08;AJAX&#xff09;是美国国家航空航天局艾姆斯研究中心与 H211, L.L.C. 公司的合作项目&#xff0c;旨在促进对加利福尼亚、内华达和太平洋沿岸地区的…

从0开始带你成为Kafka消息中间件高手---第一讲

从0开始带你成为Kafka消息中间件高手—第一讲 网站的用户行为日志&#xff0c;假设电商网站&#xff0c;我现在需要买一个阅读架&#xff0c;看书的架子 京东&#xff0c;我平时比较喜欢用的是京东&#xff0c;送货很快&#xff0c;自营商品&#xff0c;都是放在自己的仓库里…

【字典树(前缀树) 异或 离线查询】1707. 与数组中元素的最大异或值

本文涉及知识点 字典树&#xff08;前缀树&#xff09; 位运算 异或 离线查询 LeetCode1707. 与数组中元素的最大异或值 给你一个由非负整数组成的数组 nums 。另有一个查询数组 queries &#xff0c;其中 queries[i] [xi, mi] 。 第 i 个查询的答案是 xi 和任何 nums 数组…

阿里巴巴最新研究突破:自我演化大模型,打破性能天花板

获取本文论文原文PDF&#xff0c;请在公众号【AI论文解读】留言&#xff1a;论文解读AI论文解读 原创作者 | 柏企 引言&#xff1a;自我进化的新篇章 在人工智能领域&#xff0c;大型语言模型&#xff08;LLMs&#xff09;的发展正迎来一场革命性的变革。传统的训练模式依赖…

从0开始学统计-方差分析

1.什么是方差分析&#xff1f; 方差分析&#xff08;ANOVA&#xff0c;Analysis of Variance&#xff09;是一种统计方法&#xff0c;用于比较三个或三个以上组之间的平均值是否存在显著差异。它适用于以下情况&#xff1a; &#xff08;1&#xff09; 当我们有三个或三个以上…

LLMs之PEFT之Llama-2:《LoRA Learns Less and Forgets LessLoRA学得更少但遗忘得也更少》翻译与解读

LLMs之PEFT之Llama-2&#xff1a;《LoRA Learns Less and Forgets LessLoRA学得更少但遗忘得也更少》翻译与解读 导读&#xff1a;该论文比较了LoRA与完全微调在代码与数学两个领域的表现。 背景问题&#xff1a;微调大规模语言模型需要非常大的GPU内存。LoRA这一参数高效微调方…

.NET 一款内部最新的免杀WebShell

01阅读须知 此文所提供的信息只为网络安全人员对自己所负责的网站、服务器等&#xff08;包括但不限于&#xff09;进行检测或维护参考&#xff0c;未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作。利用此文所提供的信息而造成的直接或间接后果和损失&#xf…

【Linux】Linux的基本指令_1

文章目录 二、基本指令1. whoami 和 who2. pwd3. ls4. clear5. mkdir 和 cd6. touch7. rmdir 和 rm 未完待续 二、基本指令 直接在命令行的末尾&#xff08;# 后面&#xff09;输入指令即可。在学习Linux指令的过程中&#xff0c;还会穿插一些关于Linux的知识点。 1. whoami …