最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年12月15日 15:40:22   作者:LinXunFeng  
UICollectionView是iOS中比較常見的一個控件,這篇文章主要給大家介紹了關(guān)于iOS Swift UICollectionView橫向分頁滾動,cell左右排版問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨小編來一起學(xué)習(xí)學(xué)習(xí)吧。

情況

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

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


情況分析圖

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

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

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

解決方案

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

在 LXFChatEmotionCollectionLayout.swift 中

添加一個屬性來保存所有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) 
 /// 重點在這里
 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)
 }
}

返回所有當(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)
   // 通過一系列計算, 得到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
 } 
}

附上相關(guān)項目:Swift 3.0 高仿微信

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Swift中Optional值的鏈式調(diào)用學(xué)習(xí)筆記

    Swift中Optional值的鏈式調(diào)用學(xué)習(xí)筆記

    這篇文章主要介紹了Swift中Optional值的鏈式調(diào)用學(xué)習(xí)筆記,Optional鏈是Swift入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2016-07-07
  • Swift心得筆記之函數(shù)

    Swift心得筆記之函數(shù)

    函數(shù)是執(zhí)行特定任務(wù)的代碼自包含塊。通過給定一個函數(shù)名稱標(biāo)識它是什么,并在需要的時候使用該名稱來調(diào)用函數(shù)以執(zhí)行任務(wù)。今天我們就來探討下swift中的函數(shù)問題。
    2015-04-04
  • Swift讀取App的版本信息與PCH文件詳解

    Swift讀取App的版本信息與PCH文件詳解

    這篇文章主要介紹了Swift讀取App的版本信息與PCH文件的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • Swift?Sequence?Collection使用示例學(xué)習(xí)

    Swift?Sequence?Collection使用示例學(xué)習(xí)

    這篇文章主要為大家介紹了Swift?Sequence?Collection使用示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Swift類型創(chuàng)建之自定義一個類型詳解

    Swift類型創(chuàng)建之自定義一個類型詳解

    這篇文章主要介紹了Swift類型創(chuàng)建之自定義一個類型詳解,本文講解了自定義原型、實現(xiàn)默認值、支持基本布爾型初始化、支持Bool類型判斷、支持兼容各們各派的類型、完善OCBool的布爾基因體系等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • Swift算法實現(xiàn)字符串轉(zhuǎn)數(shù)字的方法示例

    Swift算法實現(xiàn)字符串轉(zhuǎn)數(shù)字的方法示例

    最近學(xué)完了swift想著實踐下,就通過一些簡單的算法進行學(xué)習(xí)研究,下面這篇文章主要介紹了Swift算法實現(xiàn)字符串轉(zhuǎn)數(shù)字的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • Swift data范圍截取問題解決方案

    Swift data范圍截取問題解決方案

    這篇文章主要介紹了Swift data范圍截取問題解決方案,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • Swift踩坑實戰(zhàn)之一個字符引發(fā)的Crash

    Swift踩坑實戰(zhàn)之一個字符引發(fā)的Crash

    swift通常都是通過對應(yīng)的signal來捕獲crash,下面這篇文章主要給大家介紹了關(guān)于Swift踩坑實戰(zhàn)之一個字符引發(fā)的Crash的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-02-02
  • Swift4.0 Array數(shù)組詳解

    Swift4.0 Array數(shù)組詳解

    這篇文章主要為大家詳細介紹了Swift4.0 Array數(shù)組的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Swift 字符串類型及常用方法詳解總結(jié)

    Swift 字符串類型及常用方法詳解總結(jié)

    Swift 字符串是一系列字符的集合。例如 "Hello, World!" 這樣的有序的字符類型的值的集合,它的數(shù)據(jù)類型為 String,接下來文章將詳細探討
    2021-11-11

最新評論

贡山| 天峻县| 龙游县| 沿河| 绥阳县| 陕西省| 平阳县| 昭通市| 宜川县| 基隆市| 灌南县| 烟台市| 松阳县| 汪清县| 镶黄旗| 永济市| 大竹县| 揭西县| 仪陇县| 鱼台县| 蓬溪县| 颍上县| 顺昌县| 化隆| 定襄县| 福贡县| 大余县| 焉耆| 庆安县| 金沙县| 定南县| 鹤庆县| 西乌珠穆沁旗| 满洲里市| 江孜县| 南陵县| 九寨沟县| 呼伦贝尔市| 札达县| 商都县| 宝丰县|