麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Swift > 正文

iOS Swift UICollectionView橫向分頁滾動(dòng),cell左右排版問題詳解

2020-03-09 17:42:51
字體:
供稿:網(wǎng)友

情況

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

最近因?yàn)楣ぷ鞯男枨?,在做表情鍵盤時(shí)遇到一個(gè)問題,我用UICollectionView來布局表情,使用橫向分頁滾動(dòng),但在最后一頁出現(xiàn)了如圖所示的情況

uicollectionview,分頁,swift,自定義cell

情況分析圖

是的,現(xiàn)在的item分布就是這個(gè)鬼樣子

uicollectionview,分頁,swift,自定義cell

現(xiàn)在想要做的,就是將現(xiàn)在這個(gè)鬼樣子變成另外一種樣子,如圖

uicollectionview,分頁,swift,自定義cell

那怎么辦?只好重新布局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}

大功告成

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)    // 設(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)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到swift教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄色作爱视频 | 丰满年轻岳中文字幕一区二区 | 一级一级一级一级毛片 | 久久国产免费视频 | 国产一区成人 | 国产91在线高潮白浆在线观看 | www.99re1.com| 亚洲第一成人久久网站 | 久久精品一区二区三区四区五区 | 日韩视频在线观看免费视频 | 日美黄色片| 99精品热视频| 91成人影库 | 请播放一级毛片 | 国产精品一区在线看 | 欧美一级精品片在线看 | 精品伊人 | 极品美女一级毛片 | 91精品国产91久久久久久丝袜 | 青青草最新网址 | 日韩在线欧美在线 | 国产成人高清成人av片在线看 | 婷婷久久综合九色综合色多多蜜臀 | 久久亚洲综合色 | 成人在线观看免费视频 | 欧美精品免费一区二区三区 | 色成人在线 | 国产日产精品一区四区介绍 | 国产成人高清成人av片在线看 | 欧美成人免费香蕉 | 欧美精品久久久久久久久久 | 黄色三级网站 | 成人在线网站 | 精品国产一级毛片 | 999久久久国产999久久久 | 亚洲综合网站 | 毛片三区| qyl在线视频精品免费观看 | 久草在线观看福利视频 | 久久久久久久久久久久免费 | 日韩在线毛片 |