Objectiv-c - UICollectionViewLayout自定义布局-瀑布流

瀑布流布局实现

最近刚写的一个简单的瀑布流.
整体思路可能不是很完善.
不过也算是实现效果了.
高手勿喷

瀑布流.gif

思路: 自定义UICollectionViewLayout实际上就是需要返回每个item的fram就可以了.
先说简单的,width值 = (CollectionView的整体宽度 - 左右边距 - 每列的间距 )/列数
height = 按照原图比例缩放就行
x 需要定位 在哪一列上 = 左边距 + (列间距 + width) * 最小列的列号
y 永远是在列高最小的那列下添加 = 最小列高 + 行间距
最小列高的计算需要 将每一列的列高算出来 比较下,最小列高的值是不断被替代.初始化应当就是上边距的值.
由于需要计算出最小列高于是我定义一个字典.
{ key0:columnHeight0,
key1: columnHeight0

}
字典元素的个数由 column来决定. 每列存放的是当前列的列高.
通过比较columnHeight中最小的来获得最小 key ,每次更新这个最小key对应的columnHeight就行了

  • 下面看代码:

公开变量以及代理.
公开的变量是可以进行调用时设置,一般就为这些,delegate用来实现动态的高度设置

//
//  WaterFallLayout.h
//  作业3
//
//  Created by gongwenkai on 2016/12/7.
//  Copyright © 2016年 gongwenkai. All rights reserved.
//

#import <UIKit>

@protocol  WaterFallLayoutDelegate<NSObject>

///设置图片高度
//width为cell实际宽度
- (CGFloat) collectionViewHeightAtIndexPath:(NSIndexPath *)indexPath withItemWidth:(CGFloat)width;

@end

@interface WaterFallLayout : UICollectionViewLayout

@property(nonatomic,assign)int column; //设置列数
@property(nonatomic,assign)int rowMargin; //设置行间距
@property(nonatomic,assign)int columnMargin;//设置列间距
@property(nonatomic,assign)UIEdgeInsets edge;//设置边距
@property(nonatomic,strong)id<WaterFallLayoutDelegate>delegate;
@end

由于我们的自定义布局继承UICollectionViewLayout.
每次布局都会调用
//准备布局做一些准备工作,例如初始化

- (void)prepareLayout; 
//这个方法在prepareLayout后调用.每次拖动都会调用,有点类似scrollview的那个.
//这里需要返回一个UICollectionViewLayoutAttributes数组里面就能存放fram信息
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect ;

先做准备工作:初始化字典

- (void)prepareLayout {
    [super prepareLayout];

//    NSLog(@"prepareLayout");

    //初始化字典
    for (int i = 0; i < _column; i++) {
        [self.maxYDict setObject:[NSNumber numberWithFloat:self.edge.top] forKey:[NSString stringWithFormat:@"%d",i]];
    }
    self.minKey = @"0";


    NSMutableArray *array = [NSMutableArray array];
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i<count xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> height) {
            minY = height;
            self.minKey = [NSString stringWithFormat:@"%d",i];
            columnHeight = minY + heightAtt;

        } else {
            columnHeight = height;

            [self.maxYDict setObject:[NSNumber numberWithFloat:columnHeight] forKey:[NSString stringWithFormat:@"%d",i]];

        }

    }

    //设置X,Y坐标
    CGFloat x = self.edge.left + [self.minKey floatValue] * (width + self.columnMargin);
    CGFloat y = [[self.maxYDict objectForKey:self.minKey] floatValue] ;

    //更新最小列的高度
    [self.maxYDict setObject:[NSNumber numberWithFloat:y+heightAtt] forKey:self.minKey];

    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attrs.frame = CGRectMake(x, y, width, heightAtt);

    return attrs;
}

计算完了就可以让layoutAttributesForElementsInRect设置回布局了

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
//    NSLog(@"layoutAttributesForElementsInRect");

    return self.attrsArray;
}

最后我们需要重写一下内容的范围,高为最大的列高

/*
 重写 设置collectionViewContentSize
 */
- (CGSize)collectionViewContentSize {

    //最高列关键字
    int columnHeight = 0;
    //默认取第一个元素
    float maxY = [[self.maxYDict objectForKey:@"0"] floatValue];
    //找到字典中最大的数
    for (int i = 0; i < self.maxYDict.allKeys.count; i++) {
        float height = [[self.maxYDict objectForKey:[NSString stringWithFormat:@"%d",i]] floatValue];
        if (maxY < height) {
            //保持maxY最小
            maxY = height;
            //记录key
            columnHeight = i;
        }
    }

    //读取最高列
    CGFloat maxHeight = [[self.maxYDict objectForKey:[NSString stringWithFormat:@"%d",columnHeight]] floatValue];

    return CGSizeMake(0, maxHeight + self.edge.bottom);
}

大功告成。

Demo地址

https://github.com/gongxiaokai/WaterFallLayoutDemo

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值