gpt4 book ai didi

swift - 确定 Collection View 单元格的大小

转载 作者:行者123 更新时间:2023-11-28 15:00:10 25 4
gpt4 key购买 nike

我正在尝试调整 Collection View 中特定单元格的大小。好的,我正在尝试制作如下所示的布局。

enter image description here

黑色的图片应该是一张图片。所以它应该是 1 个大图像然后 2 个小图像。好的,假设我有 30 张图像。现在我想要位于 (0,4,6,10,12,16,18,22,24,28,30...) 的单元格,这些是我想要大图像但遇到麻烦的单元格。你可以看到它上升了 4,然后上升了 2,依此类推。将不胜感激任何帮助。 :)

最佳答案

我已经使用 UICollectionViewLayout 来获得该设计。我已经用基本逻辑 let perRowHeight : CGFloat = 190 为每个单元格更改了 CGRect。在 perRowHeight 中,我添加了三个单元格。那就是,一个大小区和两个小小区。

在您的设计中,有六种差异类型。所以我将 indexPath.item 除以 6。

编码

class ImageDesignLayout: UICollectionViewLayout {

var CellWidth : CGFloat?
var CellHeight : CGFloat?
var cellAttrsDictionary = Dictionary<IndexPath, UICollectionViewLayoutAttributes>()
var contentSize = CGSize.zero

let perRowHeight : CGFloat = 190
var rowCountValue : Int = 0




override var collectionViewContentSize : CGSize {
return self.contentSize
}

override func prepare() {

var portrait_Ypos : Double = 0.0
var xPos : Double = 0.0
var CELL_WIDTH : Double = 0.0
var CELL_HEIGHT : Double = 0.0
rowCountValue = 0

if let sectionCount = collectionView?.numberOfSections, sectionCount > 0
{
for section in 0...sectionCount-1
{
if let rowCount = collectionView?.numberOfItems(inSection: section), rowCount > 0
{
for item in 0...rowCount-1
{

let cellIndex = IndexPath(item: item, section: 0)

let itemFactor = item % 6

print("itemFactoritemFactor ", itemFactor)

switch itemFactor
{
case 0: // BIG ITEM - LEFT

xPos = 0
portrait_Ypos = Double(perRowHeight * CGFloat(rowCountValue))
CELL_WIDTH = Double(Int((collectionView?.frame.size.width)! * (3/4)))


CELL_HEIGHT = Double(perRowHeight)
rowCountValue = rowCountValue + 1

break
case 1:

xPos = Double(Int((collectionView?.frame.size.width)! * (3/4)))
portrait_Ypos = portrait_Ypos + 0
CELL_WIDTH = Double((collectionView?.frame.size.width)!) - xPos
CELL_HEIGHT = Double(perRowHeight / 2)


break
case 2:
xPos = Double(Int((collectionView?.frame.size.width)! * (3/4)))
portrait_Ypos = portrait_Ypos + Double(perRowHeight / 2)
CELL_WIDTH = Double((collectionView?.frame.size.width)!) - xPos
CELL_HEIGHT = Double(perRowHeight / 2)

break
case 3:

xPos = 0
portrait_Ypos = Double(perRowHeight * CGFloat(rowCountValue))
CELL_WIDTH = Double(Int((collectionView?.frame.size.width)! * (1/4)))
CELL_HEIGHT = Double(perRowHeight / 2)

break
case 4:

xPos = 0
portrait_Ypos = portrait_Ypos + Double(perRowHeight / 2)
CELL_WIDTH = Double(Int((collectionView?.frame.size.width)! * (1/4)))
CELL_HEIGHT = Double(perRowHeight / 2)

break
case 5: // BIG ITEM - Right

xPos = Double(Int((collectionView?.frame.size.width)! * (1/4)))
portrait_Ypos = Double(perRowHeight * CGFloat(rowCountValue))
CELL_WIDTH = Double((collectionView?.frame.size.width)!) - xPos
CELL_HEIGHT = Double(perRowHeight)

rowCountValue = rowCountValue + 1

break
default:
break
}

let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex)
cellAttributes.frame = CGRect(x: xPos, y: portrait_Ypos, width: CELL_WIDTH, height: CELL_HEIGHT)

cellAttrsDictionary[cellIndex] = cellAttributes
}
}
}
}

let contentWidth = UIScreen.main.bounds.width
let contentHeight = CGFloat(portrait_Ypos) + CGFloat(perRowHeight)
self.contentSize = CGSize(width: contentWidth, height: contentHeight)

print("sPort.contentSize ", self.contentSize)
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

var attributesInRect = [UICollectionViewLayoutAttributes]()

for cellAttributes in cellAttrsDictionary.values {
if rect.intersects(cellAttributes.frame) {

attributesInRect.append(cellAttributes)

}
}

return attributesInRect
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

return cellAttrsDictionary[indexPath]!

}



override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}

}

Storyboard

enter image description here

输出

enter image description here

关于swift - 确定 Collection View 单元格的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com