Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionViewLayout水平剖面、垂直单元格_Ios_Swift_Uicollectionview_Uicollectionviewlayout - Fatal编程技术网

Ios UICollectionViewLayout水平剖面、垂直单元格

Ios UICollectionViewLayout水平剖面、垂直单元格,ios,swift,uicollectionview,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewlayout,我想使用带有自定义布局的UICollectionView,其中集合的部分水平滚动,部分中的项目垂直滚动 目前,我使用的是UICollectionView,然后是具有自己集合视图的单元格,但希望实现更简洁的实现,我想知道是否可以使用自定义UICollectionViewLayout。我上载了带有自定义集合视图和自定义流布局的objective C项目,您可以从这里下载,但如果您愿意的话swift我还粘贴了两个在swift 3.0上翻译的主要类 导入UIKit 类CustomCollectionV

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


目前,我使用的是UICollectionView,然后是具有自己集合视图的单元格,但希望实现更简洁的实现,我想知道是否可以使用自定义UICollectionViewLayout。

我上载了带有自定义集合视图和自定义流布局的objective C项目,您可以从这里下载,但如果您愿意的话swift我还粘贴了两个在swift 3.0上翻译的主要类

导入UIKit
类CustomCollectionViewFlowLayout:UICollectionViewFlowLayout{
var itemWidth:CGFloat=20
var itemHeight:CGFloat=20
变量空间:CGFloat=10
变量列:Int{
返回self.collectionView!.numberOfItems(第0节)
}
变量行:Int{
返回self.collectionView!.numberOfSections
}
init(itemWidth:CGFloat=20,itemHeight:CGFloat=20,space:CGFloat=5){
self.itemWidth=itemWidth
self.itemHeight=itemHeight
self.space=空间
super.init()
}
必需的初始化(编码器aDecoder:NSCoder){
self.itemWidth=20
self.itemHeight=20
self.space=3
super.init()
}
覆盖变量collectionViewContentSize:CGSize{
设w:CGFloat=CGFloat(列)*(itemWidth+空格)-500
设h:CGFloat=CGFloat(行)*(项目高度+空间)
返回CGSize(宽:w,高:h)
}
重写func layoutAttributesForItem(在indexPath:indexPath)->UICollectionViewLayoutAttributes{
let attributes=UICollectionViewLayoutAttribute(forCellWith:indexPath)
设x:CGFloat=CGFloat(indexath.row)*(itemWidth+space)
设y:CGFloat=CGFloat(indexath.section)+CGFloat(indexath.section)*(itemHeight+space)
attributes.frame=CGRect(x:x,y:y,width:itemWidth+CGFloat(indexath.row),height:itemHeight)
返回属性
}
是否覆盖func layoutAttributesForElements(在rect:CGRect中)->[UICollectionViewLayoutAttributes]{
设minRow:Int=(rect.origin.x>0)?Int(floor(rect.origin.x/(itemWidth+space)):0
设maxRow:Int=min(列-1,Int(ceil(rect.size.width/(itemWidth+space))+CGFloat(minRow)))
变量属性:数组=[UICollectionViewLayoutAttribute]()
对于0..<行中的i{
对于minRow中的j…maxRow{
attributes.append(self.layouttributesforItem(at:indepath(item:j,section:i))!)
}
}
返回属性
}
导入UIKit
类CustomCollectionView:UICollectionView{
覆盖func布局子视图(){
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(浮点(CGFloat(currentOffset.x-centerOffsetX));
var distanceFromCenterY=fabsf(Float(CGFloat(currentOffset.y-centerOffsetY));
如果(CGFloat(distanceFromCenterX)>contentWidth/4.0){//这个4.0的数字是任意的
//self.contentOffset=CGPoint(x:centerOffsetX,y:currentOffset.y);
}
如果(CGFloat(距离中心)>内容高度/4.0){
//self.contentOffset=CGPoint(x:currentOffset.x,y:centerOffsetY);
}
}
}

我认为自定义布局非常适合您。我可以为您提供示例代码和带有已实现自定义布局的项目,看看它是如何工作的。您喜欢swift还是objective C实现?Sweet,我对API不太熟悉,目前正沉浸在它的文档中:)-swift对我很好:)谢谢!我上传了objective C p项目,但如果您有问题,欢迎提问:)
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);
        }
    }
}