我正在尝试调整集合视图中的特定单元格。好吧,我正试图制作一个看起来像这样的布局。
黑色图像应为一张图像。所以它应该是1个大图像然后是2个小图像。好吧,让我说我有30张图片。现在我想要(0,4,6,10,12,16,18,22,24,28,30 ......)的细胞,这些细胞我想要有一个大图像,但有麻烦。你可以看到它上升4而不是2,依此类推。非常感谢任何帮助。 :)
答案 0 :(得分:1)
我使用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
}
}
<强>故事板强>
<强>输出强>