iOS Swift UICollectionView橫向分頁滾動,cell左右排版

狀況

最近在作表情鍵盤時遇到一個問題,我用UICollectionView來佈局表情,使用橫向分頁滾動,但在最後一頁出現瞭如圖所示的狀況 git

只顯示一半

狀況分析圖

是的,如今的item分佈就是這個鬼樣子 github

從上到下,從左到右
如今想要作的,就是將如今這個鬼樣子變成另一種樣子,如圖
從左到右,從上到下
那怎麼辦?只好從新佈局item了

解決方案

我是自定了一個Layout(LXFChatEmotionCollectionLayout),讓UICollectionView在建立的時候使用了它swift

在 LXFChatEmotionCollectionLayout.swift 中微信

添加一個屬性來保存全部item的attributes

// 保存全部item的attributes
fileprivate var attributesArr: [UICollectionViewLayoutAttributes] = []
複製代碼

從新佈局

// MARK:- 從新佈局
override func prepare() {
    super.prepare()
    
    let itemWH: CGFloat = kScreenW / CGFloat(kEmotionCellNumberOfOneRow)
    
    // 設置itemSize
    itemSize = CGSize(width: itemWH, height: itemWH)
    minimumLineSpacing = 0
    minimumInteritemSpacing = 0
    scrollDirection = .horizontal
    
    // 設置collectionView屬性
    collectionView?.isPagingEnabled = true
    collectionView?.showsHorizontalScrollIndicator = false
    collectionView?.showsVerticalScrollIndicator = true
    let insertMargin = (collectionView!.bounds.height - 3 * itemWH) * 0.5
    collectionView?.contentInset = UIEdgeInsetsMake(insertMargin, 0, insertMargin, 0)
    
    
    /// 重點在這裏
    var page = 0
    let itemsCount = collectionView?.numberOfItems(inSection: 0) ?? 0
    for itemIndex in 0..<itemsCount {
        let indexPath = IndexPath(item: itemIndex, section: 0)
        let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
        
        page = itemIndex / (kEmotionCellNumberOfOneRow * kEmotionCellRow)
        // 經過一系列計算, 獲得x, y值
        let x = itemSize.width * CGFloat(itemIndex % Int(kEmotionCellNumberOfOneRow)) + (CGFloat(page) * kScreenW)
        let y = itemSize.height * CGFloat((itemIndex - page * kEmotionCellRow * kEmotionCellNumberOfOneRow) / kEmotionCellNumberOfOneRow)
        
        attributes.frame = CGRect(x: x, y: y, width: itemSize.width, height: itemSize.height)
        // 把每個新的屬性保存起來
        attributesArr.append(attributes)
    }
}
複製代碼

返回全部當前可見的Attributes

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var rectAttributes: [UICollectionViewLayoutAttributes] = []
    _ = attributesArr.map({
        if rect.contains($0.frame) {
            rectAttributes.append($0)
        }
    })
    return rectAttributes
}
複製代碼

大功告成

完整代碼

import UIKit

let kEmotionCellNumberOfOneRow = 8
let kEmotionCellRow = 3

class LXFChatEmotionCollectionLayout: UICollectionViewFlowLayout {
    // 保存全部item
    fileprivate var attributesArr: [UICollectionViewLayoutAttributes] = []
    
    // MARK:- 從新佈局
    override func prepare() {
        super.prepare()
        
        let itemWH: CGFloat = kScreenW / CGFloat(kEmotionCellNumberOfOneRow)
        
        // 設置itemSize
        itemSize = CGSize(width: itemWH, height: itemWH)
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = .horizontal
        
        // 設置collectionView屬性
        collectionView?.isPagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = true
        let insertMargin = (collectionView!.bounds.height - 3 * itemWH) * 0.5
        collectionView?.contentInset = UIEdgeInsetsMake(insertMargin, 0, insertMargin, 0)
        
        var page = 0
        let itemsCount = collectionView?.numberOfItems(inSection: 0) ?? 0
        for itemIndex in 0..<itemsCount {
            let indexPath = IndexPath(item: itemIndex, section: 0)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            
            page = itemIndex / (kEmotionCellNumberOfOneRow * kEmotionCellRow)
            // 經過一系列計算, 獲得x, y值
            let x = itemSize.width * CGFloat(itemIndex % Int(kEmotionCellNumberOfOneRow)) + (CGFloat(page) * kScreenW)
            let y = itemSize.height * CGFloat((itemIndex - page * kEmotionCellRow * kEmotionCellNumberOfOneRow) / kEmotionCellNumberOfOneRow)
            
            attributes.frame = CGRect(x: x, y: y, width: itemSize.width, height: itemSize.height)
            // 把每個新的屬性保存起來
            attributesArr.append(attributes)
        }
        
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var rectAttributes: [UICollectionViewLayoutAttributes] = []
        _ = attributesArr.map({
            if rect.contains($0.frame) {
                rectAttributes.append($0)
            }
        })
        return rectAttributes
    }
    
}
複製代碼

附上相關項目:Swift 3.0 高仿微信app

微信公衆號
相關文章
相關標籤/搜索