瀑布流效果(图片自适应)

版权声明:本文为Trinity原创文章,未经Trinity允许不得转载 https://blog.csdn.net/Caoyang_He/article/details/87983250

上学期进行了一次swift实训,组里的小姐姐们说能不能用瀑布流的效果,开始我以为和通讯录那种行表式差不多,查看xcode里面有没有支持这种效果的方法,但是找了很久没有找到。于是准备自己动手,code了一天,但是还是没有弄出来,在组里大佬的提醒下,我意识到我没有必要自己造轮子,可以用别人写好的。于是推荐了我一个很好的网站:代码库,里面有许多别人做好的案例。
于是我开始进行搜索瀑布流相关的代码,但是我下载了许多来运行的时候发现有许多是object-C来写,和swfit混用有点让人难受。当我找到一些用swfit写的瀑布流案例后,发现有些代码逻辑太乱,有些又写的很复杂,好不容易遇到一个注释清晰的,但是不能实现我想要实现的效果。最后找到了一个可塑性较好的瀑布流代码(代码链接
我想要的几个特点是:

  1. 最基本的瀑布流效果
  2. 可以有多列,至少要有两列
  3. 每个cell里面的图片可以自适应(这个就是自己写的)

基本代码如下:

WCLWaterFallLayout.swift


import UIKit

@objc protocol WCLWaterFallLayoutDelegate {
    //waterFall的列数
    func columnOfWaterFall(_ collectionView: UICollectionView) -> Int
    //每个item的高度
    func waterFall(_ collectionView: UICollectionView, layout waterFallLayout: WCLWaterFallLayout, heightForItemAt indexPath: IndexPath) -> CGFloat
}


class WCLWaterFallLayout: UICollectionViewLayout {
    //代理
    weak var delegate: WCLWaterFallLayoutDelegate?
    //行间距
    @IBInspectable var lineSpacing: CGFloat   = 20
    //列间距
    @IBInspectable var columnSpacing: CGFloat = 20
    //section的top
    @IBInspectable var sectionTop: CGFloat    = 0 {
        willSet {
            sectionInsets.top = newValue
        }
    }
    //section的Bottom
    @IBInspectable var sectionBottom: CGFloat  = 0 {
        willSet {
            sectionInsets.bottom = newValue
        }
    }
    //section的left
    @IBInspectable var sectionLeft: CGFloat   = 0 {
        willSet {
            sectionInsets.left = newValue
        }
    }
    //section的right
    @IBInspectable var sectionRight: CGFloat  = 0 {
        willSet {
            sectionInsets.right = newValue
        }
    }
    //section的Insets
    @IBInspectable var sectionInsets: UIEdgeInsets      = UIEdgeInsets.zero
    //每行对应的高度
    private var columnHeights: [Int: CGFloat]                  = [Int: CGFloat]()
    private var attributes: [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
    
    //MARK: Initial Methods
    init(lineSpacing: CGFloat, columnSpacing: CGFloat, sectionInsets: UIEdgeInsets) {
        super.init()
        self.lineSpacing      = lineSpacing
        self.columnSpacing    = columnSpacing
        self.sectionInsets    = sectionInsets
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    //MARK: Public Methods
    
    
    //MARK: Override
    override var collectionViewContentSize: CGSize {
        var maxHeight: CGFloat = 0
        for height in columnHeights.values {
            if height > maxHeight {
                maxHeight = height
            }
        }
        return CGSize.init(width: collectionView?.frame.width ?? 0, height: maxHeight + sectionInsets.bottom)
    }
    
    override func prepare() {
        super.prepare()
        guard collectionView != nil else {
            return
        }
        if let columnCount = delegate?.columnOfWaterFall(collectionView!) {
            for i in 0..<columnCount {
                columnHeights[i] = sectionInsets.top
            }
        }
        let itemCount = collectionView!.numberOfItems(inSection: 0)
        attributes.removeAll()
        for i in 0..<itemCount {
            if let att = layoutAttributesForItem(at: IndexPath.init(row: i, section: 0)) {
                attributes.append(att)
            }
        }
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        if let collectionView = collectionView {
            //根据indexPath获取item的attributes
            let att = UICollectionViewLayoutAttributes.init(forCellWith: indexPath)
            //获取collectionView的宽度
            let width = collectionView.frame.width
            if let columnCount = delegate?.columnOfWaterFall(collectionView) {
                guard columnCount > 0 else {
                    return nil
                }
                //item的宽度 = (collectionView的宽度 - 内边距与列间距) / 列数
                let totalWidth  = (width - sectionInsets.left - sectionInsets.right - (CGFloat(columnCount) - 1) * columnSpacing)
                let itemWidth   = totalWidth / CGFloat(columnCount)
                //获取item的高度,由外界计算得到
                let itemHeight  = delegate?.waterFall(collectionView, layout: self, heightForItemAt: indexPath) ?? 0
                //找出最短的那一列
                var minIndex = 0
                for column in columnHeights {
                    if column.value < columnHeights[minIndex] ?? 0 {
                        minIndex = column.key
                    }
                }
                //根据最短列的列数计算item的x值
                let itemX  = sectionInsets.left + (columnSpacing + itemWidth) * CGFloat(minIndex)
                //item的y值 = 最短列的最大y值 + 行间距
                let itemY  = (columnHeights[minIndex] ?? 0) + lineSpacing
                //设置attributes的frame
                att.frame  = CGRect.init(x: itemX, y: itemY, width: itemWidth, height: itemHeight)
                //更新字典中的最大y值
                columnHeights[minIndex] = att.frame.maxY
            }
            return att
        }
        return nil
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return attributes
    }
}

代码流畅,注释清晰
接着就是应用部分主要是以下部分代码

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        imageindex =  indexPath.row
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if miaoshu.count > imagenames.count
        {
            return imagenames.count
        }
        else
        {
            //return (miaoshu1?.count)!
            return miaoshu.count
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Identifier", for: indexPath)
        let color = UIColor.init(red: 255.0/255.0, green: 204.0/255.0, blue: 204.0/255.0, alpha: 0.5) 
        cell.contentView.backgroundColor = color

        //let imagename = "p"+String(indexPath.row % 6+1)
       // imagename =  String(indexPath.row % 11+1)
        if imageget != nil && indexPath.row == 0
        {
            image = imageget
        }
        else
        {
            
            imagename = imagenames[indexPath.row ]
            image = UIImage(named: imagename)
        }
        let imageheight = image?.size.height
        let imagewidth = image?.size.width
        let rate = imagewidth! / imageheight!
        let height = self.collectionView.bounds.width / rate
        
        
        
        let imageView = UIImageView(frame: CGRect(x:0, y: 1, width: self.collectionView.bounds.width/2-5, height: (height)/2))
        imageView.image = image
        imageView.layer.cornerRadius = 10
        imageView.layer.masksToBounds = true
        cell.contentView.addSubview(imageView)
        
       
        
        let labelName = UILabel(frame: CGRect(x: 0,y:height/2 + 10 , width: self.collectionView.bounds.width/2-5, height: 20))
        if miaoshuget != "" && indexPath.row == 0
        {
            labelName.text = self.miaoshuget
        }
        else
        {
            labelName.text = miaoshu[indexPath.row ]
        }
        labelName.textAlignment = .center
        labelName.font = UIFont.systemFont(ofSize: 20)
        cell.contentView.addSubview(labelName)
        
        cell.contentView.clipsToBounds      = true
        cell.contentView.layer.cornerRadius = 8
       
        return cell
        
    }
    
    func columnOfWaterFall(_ collectionView: UICollectionView) -> Int {
        return columnCount
    }
    
    func waterFall(_ collectionView: UICollectionView, layout waterFallLayout: WCLWaterFallLayout, heightForItemAt indexPath: IndexPath) -> CGFloat {
        //image = UIImage(named: String(indexPath.row % 11+1))
        if imageget != nil && indexPath.row == 0
        {
            image = imageget
        }
        else
        {
            
            image = UIImage(named: imagenames[indexPath.row ])
        }
        let imageheight = image?.size.height
        let imagewidth = image?.size.width
        let rate = imagewidth! / imageheight!
        let height = (self.collectionView.bounds.width / rate ) / 2 + 40

        return CGFloat(height)
    }

关于图片自适应的思路:首先以两列的瀑布流来看,由于列数固定,那么每个cell的宽width应该是一样的,要想每个cell里面的图片不失真(压缩或者拉伸),应该根据图片的的宽和高的比例来对应设置每个cell的高hight,于是得到一个表达式:cell_hight/cell_width=image_hight/image_wight,通过以上表达式就可以完成图片自适应的效果

猜你喜欢

转载自blog.csdn.net/Caoyang_He/article/details/87983250
今日推荐