确定集合视图单元格的大小
Determine size for collection view cell
我正在尝试调整集合视图中特定单元格的大小。好的,我正在尝试制作如下所示的布局。
黑色图片应该是一张图片。所以它应该是 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
}
}
故事板
输出
我正在尝试调整集合视图中特定单元格的大小。好的,我正在尝试制作如下所示的布局。
黑色图片应该是一张图片。所以它应该是 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
}
}
故事板
输出