iOS Swift UICollectionView橫向分頁滾動(dòng),cell左右排版問題詳解
情況
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 中
添加一個(gè)屬性來保存所有item的attributes
// 保存所有item的attributes 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) /// 重點(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 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) // 設(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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- iOS實(shí)現(xiàn)卡片式滾動(dòng)效果 iOS實(shí)現(xiàn)電影選片效果
- iOS ScrollView嵌套tableView聯(lián)動(dòng)滾動(dòng)的思路與最佳實(shí)踐
- iOS使用UICollectionView實(shí)現(xiàn)橫向滾動(dòng)照片效果
- iOS自定義時(shí)間滾動(dòng)選擇控件
- iOS自定義水平滾動(dòng)條、進(jìn)度條
- iOS自定義可展示、交互的scrollView滾動(dòng)條
- iOS仿網(wǎng)易新聞滾動(dòng)導(dǎo)航條效果
- iOS?實(shí)現(xiàn)類似抖音滾動(dòng)效果
相關(guān)文章
Swift仿微信語音通話最小化時(shí)后的效果實(shí)例代碼
這篇文章主要介紹了Swift仿微信語音通話最小化時(shí)后的效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Swift 3.0基礎(chǔ)學(xué)習(xí)之枚舉類型
枚舉在編程中很多時(shí)候要用到,在 Swift 中,枚舉具有更多的特性。下面這篇文章主要介紹了Swift 3.0基礎(chǔ)學(xué)習(xí)之枚舉類型的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03swift版webview加載網(wǎng)頁進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了swift實(shí)現(xiàn)webview加載網(wǎng)頁進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Swift中動(dòng)態(tài)調(diào)用實(shí)例方法介紹
這篇文章主要介紹了Swift中動(dòng)態(tài)調(diào)用實(shí)例方法介紹,在Swift中有一類很有意思的寫法,可以讓我們不直接使用實(shí)例來調(diào)用這個(gè)實(shí)例上的方法,而是通過類型取出這個(gè)類型的某個(gè)實(shí)例方法的簽名,然后再通過傳遞實(shí)例來拿到實(shí)際需要調(diào)用的方法,需要的朋友可以參考下2015-01-01在Swift中使用Objective-C編寫類、繼承Objective-C類
這篇文章主要介紹了在Swift中使用Objective-C編寫類、繼承Objective-C類等操作方法介紹,需要的朋友可以參考下2014-07-07SwiftUI 登錄界面布局實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了SwiftUI 登錄界面布局實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Swift使用enum抹平數(shù)組元素差異實(shí)例詳解
這篇文章主要為大家介紹了Swift使用enum抹平數(shù)組元素差異實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11