iOS自定義UICollectionViewLayout實(shí)現(xiàn)瀑布流布局
移動(dòng)端訪問(wèn)不佳,請(qǐng)?jiān)L問(wèn)我的個(gè)人博客
最近項(xiàng)目中需要用到瀑布流的效果,但是用UICollectionViewFlowLayout又達(dá)不到效果,自己動(dòng)手寫(xiě)了一個(gè)瀑布流的layout,下面是我的心路路程
先上效果圖與demo地址:

因?yàn)槭怯肬ICollectionView來(lái)實(shí)現(xiàn)瀑布流的,決定繼承UICollectionViewLayout來(lái)自定義一個(gè)layout來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單瀑布流的布局,下面是需要重寫(xiě)的方法:
重寫(xiě)這個(gè)屬性得出UICollectionView的ContentSize:collectionViewContentSize
重寫(xiě)這個(gè)方法來(lái)得到每個(gè)item的布局:layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
重寫(xiě)這個(gè)方法給UICollectionView所有item的布局:layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
重寫(xiě)這個(gè)方法來(lái)實(shí)現(xiàn)UICollectionView前的操作:prepare()
實(shí)現(xiàn)思路
通過(guò)代理模式獲得到需要的列數(shù)和每一item的高度,用過(guò)列數(shù)與列之間的間隔和UICollectionView的寬度來(lái)得出每一列的寬度,item從左邊到右布局,下一列的item放到高度最小的列下面,防止每列的高度不均勻,下面貼上代碼和注釋:
import UIKit
@objc protocol WCLWaterFallLayoutDelegate {
//waterFall的列數(shù)
func columnOfWaterFall(_ collectionView: UICollectionView) -> Int
//每個(gè)item的高度
func waterFall(_ collectionView: UICollectionView, layout waterFallLayout: WCLWaterFallLayout, heightForItemAt indexPath: IndexPath) -> CGFloat
}
class WCLWaterFallLayout: UICollectionViewLayout {
//代理
weak var delegate: WCLWaterFallLayoutDelegate?
//行間距
@IBInspectable var lineSpacing: CGFloat = 0
//列間距
@IBInspectable var columnSpacing: CGFloat = 0
//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
//每行對(duì)應(yīng)的高度
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 {
//根據(jù)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的寬度 - 內(nèi)邊距與列間距) / 列數(shù)
let totalWidth = (width - sectionInsets.left - sectionInsets.right - (CGFloat(columnCount) - 1) * columnSpacing)
let itemWidth = totalWidth / CGFloat(columnCount)
//獲取item的高度,由外界計(jì)算得到
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
}
}
//根據(jù)最短列的列數(shù)計(jì)算item的x值
let itemX = sectionInsets.left + (columnSpacing + itemWidth) * CGFloat(minIndex)
//item的y值 = 最短列的最大y值 + 行間距
let itemY = (columnHeights[minIndex] ?? 0) + lineSpacing
//設(shè)置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
}
}
最后附帶demo地址,大家喜歡的話可以star一下
上面是簡(jiǎn)單的瀑布流的實(shí)現(xiàn)過(guò)程,希望大家能學(xué)到東西,有很多地方考慮的不足,歡迎大家交流學(xué)習(xí),謝謝大家的閱讀。
- iOS UICollectionView刷新時(shí)閃屏的解決方法
- iOS 通過(guò)collectionView實(shí)現(xiàn)照片刪除功能
- iOS中關(guān)于Swift UICollectionView橫向分頁(yè)的問(wèn)題
- 使用iOS控件UICollectionView生成可拖動(dòng)的桌面的實(shí)例
- IOS collectionViewCell防止復(fù)用的兩種方法
- iOScollectionView廣告無(wú)限滾動(dòng)實(shí)例(Swift實(shí)現(xiàn))
- iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果
- IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView
- ios的collection控件的自定義布局實(shí)現(xiàn)與設(shè)計(jì)
相關(guān)文章
iOS中利用CAGradientLayer繪制漸變色的方法實(shí)例
有時(shí)候iOS開(kāi)發(fā)中需要使用到漸變色,來(lái)給圖片或者view蓋上一層,使其顯示效果更好,所以這篇文章主要給大家介紹了關(guān)于iOS中利用CAGradientLayer繪制漸變色的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11
詳解iOS中多線程app開(kāi)發(fā)的GCD隊(duì)列的使用
這篇文章主要介紹了詳解iOS中多線程app開(kāi)發(fā)的GCD隊(duì)列的使用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS中大尺寸圖片的旋轉(zhuǎn)與縮放實(shí)例詳解
圖片縮小旋轉(zhuǎn)是我們?cè)陂_(kāi)發(fā)中經(jīng)常會(huì)遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于iOS中大尺寸圖片的旋轉(zhuǎn)與縮放的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧2018-09-09
iOS利用AFNetworking實(shí)現(xiàn)文件上傳的示例代碼
本篇文章主要介紹了iOS利用AFNetworking實(shí)現(xiàn)文件上傳的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
iOS 使用Moya網(wǎng)絡(luò)請(qǐng)求的實(shí)現(xiàn)方法
這篇文章主要介紹了iOS 使用Moya網(wǎng)絡(luò)請(qǐng)求的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
iOS 11 使用兩種方法替換(Method Swizzling)去掉導(dǎo)航欄返回按鈕的文字
這篇文章主要介紹了iOS 11 使用方法替換(Method Swizzling)去掉導(dǎo)航欄返回按鈕的文字,需要的朋友可以參考下2018-05-05
react-native中AsyncStorage實(shí)例詳解
這篇文章主要介紹了react-native中AsyncStorage實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS使用GCDSocketManager實(shí)現(xiàn)長(zhǎng)連接的方法
下面想就為大家分享一篇iOS使用GCDSocketManager實(shí)現(xiàn)長(zhǎng)連接的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

