国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

iOS Swift UICollectionView橫向分頁滾動,cell左右排版問題詳解

2020-03-09 17:42:51
字體:
來源:轉載
供稿:網友

情況

Swift對于一門新的iOS編程語言,他的崛起是必然的,我們這群老程序員們學習新的技能也是必然的,不接受新技能將被這大群體無情的淘汰。

最近因為工作的需求,在做表情鍵盤時遇到一個問題,我用UICollectionView來布局表情,使用橫向分頁滾動,但在最后一頁出現了如圖所示的情況

uicollectionview,分頁,swift,自定義cell

情況分析圖

是的,現在的item分布就是這個鬼樣子

uicollectionview,分頁,swift,自定義cell

現在想要做的,就是將現在這個鬼樣子變成另外一種樣子,如圖

uicollectionview,分頁,swift,自定義cell

那怎么辦?只好重新布局item了

解決方案

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

在 LXFChatEmotionCollectionLayout.swift/292074.html">swift 中

添加一個屬性來保存所有item的attributes

// 保存所有item的attributesfileprivate 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}

大功告成

uicollectionview,分頁,swift,自定義cell

完整代碼

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)    // 設置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 高仿微信

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 察雅县| 武穴市| 金山区| 化州市| 锡林郭勒盟| 深州市| 泗阳县| 罗平县| 富蕴县| 乌苏市| 伊宁县| 化州市| 兰西县| 洪雅县| 陆川县| 巴彦淖尔市| 家居| 莱芜市| 阿坝县| 屏边| 凤城市| 宁夏| 广德县| 桃江县| 资兴市| 巍山| 孟津县| 富顺县| 遵义县| 扎兰屯市| 兴安盟| 江门市| 南京市| 汤阴县| 义马市| 犍为县| 泰顺县| 墨江| 贵溪市| 阜新市| 腾冲县|