2017-06-14 103 views
0

我想使用带有自定义布局的UICollectionView,其方式为集合的部分水平滚动并且部分中的项目垂直滚动。UICollectionViewLayout水平部分,垂直单元格

目前我正在使用UICollectionView,然后使用自己的集合视图的单元格,但希望做出更清晰的实现,并且想知道是否可以使用自定义UICollectionViewLayout。 enter image description here

+1

两大类我认为自定义布局对你来说是完美的。我可以给你示例代码和项目实施自定义布局,看看它是如何工作的。你更喜欢快速还是客观的C实现? –

+0

甜,我不熟悉的API,目前在它的文档丢失:) - 斯威夫特对我很好:)谢谢! –

+0

我上传目标C项目,但如果您有问题,欢迎您问:) –

回答

0

我上传自定义集合视图和自定义流布局Objective C的项目,你可以从这里下载,但如果你喜欢快速的我也贴翻译对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); 
     } 
    } 
}