情況
Swift對(duì)于一門新的iOS編程語言,他的崛起是必然的,我們這群老程序員們學(xué)習(xí)新的技能也是必然的,不接受新技能將被這大群體無情的淘汰。
最近因?yàn)楣ぷ鞯男枨?,在做表情鍵盤時(shí)遇到一個(gè)問題,我用UICollectionView來布局表情,使用橫向分頁滾動(dòng),但在最后一頁出現(xiàn)了如圖所示的情況
情況分析圖
是的,現(xiàn)在的item分布就是這個(gè)鬼樣子
現(xiàn)在想要做的,就是將現(xiàn)在這個(gè)鬼樣子變成另外一種樣子,如圖
那怎么辦?只好重新布局item了
解決方案
我是自定了一個(gè)Layout(LXFChatEmotionCollectionLayout) ,讓UICollectionView在創(chuàng)建的時(shí)候使用了它
在 LXFChatEmotionCollectionLayout.swift/292074.html">swift 中
添加一個(gè)屬性來保存所有item的attributes
// 保存所有item的attributesfileprivate var attributesArr: [UICollectionViewLayoutAttributes] = []
重新布局
// MARK:- 重新布局override func prepare() { super.prepare() let itemWH: CGFloat = kScreenW / CGFloat(kEmotionCellNumberOfOneRow) // 設(shè)置itemSize itemSize = CGSize(width: itemWH, height: itemWH) minimumLineSpacing = 0 minimumInteritemSpacing = 0 scrollDirection = .horizontal // 設(shè)置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) /// 重點(diǎn)在這里 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) // 通過一系列計(jì)算, 得到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) // 把每一個(gè)新的屬性保存起來 attributesArr.append(attributes) }}
返回所有當(dāng)前可見的Attributes
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var rectAttributes: [UICollectionViewLayoutAttributes] = [] _ = attributesArr.map({ if rect.contains($0.frame) { rectAttributes.append($0) } }) return rectAttributes}
大功告成
完整代碼
import UIKitlet kEmotionCellNumberOfOneRow = 8let kEmotionCellRow = 3class LXFChatEmotionCollectionLayout: UICollectionViewFlowLayout { // 保存所有item fileprivate var attributesArr: [UICollectionViewLayoutAttributes] = [] // MARK:- 重新布局 override func prepare() { super.prepare() let itemWH: CGFloat = kScreenW / CGFloat(kEmotionCellNumberOfOneRow) // 設(shè)置itemSize itemSize = CGSize(width: itemWH, height: itemWH) minimumLineSpacing = 0 minimumInteritemSpacing = 0 scrollDirection = .horizontal // 設(shè)置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) // 通過一系列計(jì)算, 得到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) // 把每一個(gè)新的屬性保存起來 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 } }
附上相關(guān)項(xiàng)目:Swift 3.0 高仿微信
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。
|
新聞熱點(diǎn)
疑難解答
圖片精選