gpt4 book ai didi

ios - UICollectionViewLayout 水平部分,垂直单元格

转载 作者:行者123 更新时间:2023-11-30 12:23:08 25 4
gpt4 key购买 nike

我想使用具有自定义布局的 UICollectionView,其中集合的部分水平滚动,部分中的项目垂直滚动。

目前,我正在使用 UICollectionView,然后使用具有自己的 Collection View 的单元格,但希望进行更清晰的实现,并且想知道是否可以使用自定义 UICollectionViewLayout。 enter image description here

最佳答案

我上传了带有自定义 Collection View 和自定义流程布局的 Objective C 项目,您可以从此处下载,但如果您更喜欢 swift,我还粘贴了在 swift 3.0 上翻译的两个主要类

http://www.filedropper.com/collectionviewinfinitescrollintwodimentions

import UIKit
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
var itemWidth:CGFloat = 20
var itemHeight:CGFloat = 20
var space:CGFloat = 10

var columns: Int{
return self.collectionView!.numberOfItems(inSection: 0)
}
var rows: Int{
return self.collectionView!.numberOfSections
}


init(itemWidth: CGFloat = 20, itemHeight: CGFloat = 20, space: CGFloat = 5) {
self.itemWidth = itemWidth
self.itemHeight = itemHeight
self.space = space
super.init()
}

required init(coder aDecoder: NSCoder) {
self.itemWidth = 20
self.itemHeight = 20
self.space = 3
super.init()
}

override var collectionViewContentSize: CGSize{
let w : CGFloat = CGFloat(columns) * (itemWidth + space) - 500
let h : CGFloat = CGFloat(rows) * (itemHeight + space)
return CGSize(width: w, height: h)
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
let x : CGFloat = CGFloat(indexPath.row) * (itemWidth + space)
let y : CGFloat = CGFloat(indexPath.section) + CGFloat(indexPath.section) * (itemHeight + space)
attributes.frame = CGRect(x: x, y: y, width: itemWidth + CGFloat(indexPath.row), height: itemHeight)
return attributes
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let minRow : Int = (rect.origin.x > 0) ? Int(floor(rect.origin.x/(itemWidth + space))) : 0
let maxRow : Int = min(columns - 1, Int(ceil(rect.size.width / (itemWidth + space)) + CGFloat(minRow)))
var attributes : Array<UICollectionViewLayoutAttributes> = [UICollectionViewLayoutAttributes]()
for i in 0 ..< rows {
for j in minRow ... maxRow {
attributes.append(self.layoutAttributesForItem(at: IndexPath(item: j, section: i))!)
}
}
return attributes
}




import UIKit
class CustomCollectionView: UICollectionView {

override func layoutSubviews(){
super.layoutSubviews()
var currentOffset = self.contentOffset;
var contentWidth = self.contentSize.width;
var contentHeight = self.contentSize.height;
var centerOffsetX = (contentWidth - self.bounds.size.width) / 2.0;
var centerOffsetY = (contentHeight - self.bounds.size.height) / 2.0;
var distanceFromCenterX = fabsf(Float(CGFloat(currentOffset.x - centerOffsetX)));
var distanceFromCenterY = fabsf(Float(CGFloat(currentOffset.y - centerOffsetY)));

if (CGFloat(distanceFromCenterX) > contentWidth / 4.0) { // this number of 4.0 is arbitrary
//self.contentOffset = CGPoint(x:centerOffsetX, y:currentOffset.y);
}
if (CGFloat(distanceFromCenterY) > contentHeight/4.0) {
//self.contentOffset = CGPoint(x:currentOffset.x, y:centerOffsetY);
}
}
}

关于ios - UICollectionViewLayout 水平部分,垂直单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44545629/

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