zoukankan      html  css  js  c++  java
  • 瀑布流的简单实现

    详细代码请前往: https://git.oschina.net/null_248_6948/WaterfallLayout

     

    extension WaterfallLayout {

        //     prepare准备所有Cell的布局样式

        override func prepare() {

            super.prepare()

            

            //         0.获取item的个数

            let itemCount = collectionView!.numberOfItems(inSection: 0)

            

            //         1.获取列数

            let cols = dataSource?.numberOfColsInWaterfallLayout?(self) ?? 2

            

            //         2.计算Item的宽度

            let itemW = (collectionView!.bounds.width - self.sectionInset.left - self.sectionInset.right - self.minimumInteritemSpacing * CGFloat((cols - 1))) / CGFloat(cols)

            

            //         3.计算所有的item的属性

            for i in startIndex..<itemCount {

                //             1.设置每一个Item位置相关的属性

                let indexPath = IndexPath(item: i, section: 0)

                

                //             2.根据位置创建Attributes属性

                let attrs = UICollectionViewLayoutAttributes(forCellWith: indexPath)

                

                //             3.随机一个高度

                guard let height = dataSource?.waterfallLayout(self, indexPath: indexPath) else {

                    fatalError("请设置数据源,并且实现对应的数据源方法")

                }

                

                //             4.取出最小列的位置

                var minH = colHeights.min()!

                let index = colHeights.index(of: minH)!

                minH = minH + height + minimumLineSpacing

                colHeights[index] = minH

                

                //             5.设置item的属性

                attrs.frame = CGRect(x: self.sectionInset.left + (self.minimumInteritemSpacing + itemW) * CGFloat(index), y: minH - height - self.minimumLineSpacing, itemW, height: height)

                attrsArray.append(attrs)

            }

            

            //         4.记录最大值

            maxH = colHeights.max()!

            

            //         5.startIndex重新复制

            startIndex = itemCount

        }

    }

    完事皆项目,感兴趣可联系我邮箱~hjjuny@163.com
  • 相关阅读:
    [nRF51822] 1、一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO
    [安卓] 18、一个简单的例子做自定义动画按钮和自定义Actionbar
    [自己动手玩黑科技] 1、小黑科技——如何将普通的家电改造成可以与手机App联动的“智能硬件”
    [安卓] 17、一个简单的例子学安卓侧滑设计——用开源slidingmenu
    [安卓] 16、ListView和GridView结合显示单元实现自定义列表显示效果
    mysql常用处理时间的相关函数
    Mysql中HAVING的相关使用方法
    mysql中常见正则表达式的应用
    Linux系统模拟发送HTTP的get和post请求
    mysql删除字符串的前后的空格
  • 原文地址:https://www.cnblogs.com/hjjun/p/6123640.html
Copyright © 2011-2022 走看看