2017-06-14 18 views
0

UICollectionViewをカスタムレイアウトで使用すると、コレクションのセクションが水平方向にスクロールし、セクション内のアイテムが垂直方向にスクロールするようになります。UICollectionViewLayout水平セクション、垂直セル

現在、私はUICollectionViewを使用していて、独自のコレクションビューを持っていますが、よりクリーンな実装をしたいと思いますし、カスタムUICollectionViewLayoutを使用することが可能かどうか疑問に思っています。 enter image description here

+1

に翻訳された2つの主なクラスを貼り付けるカスタムコレクションビューとカスタムフローレイアウトで客観Cプロジェクトをアップロード私はカスタムレイアウトがあなたにぴったりだと思います。どのように動作するかを確認するために、実装されたカスタムレイアウトでサンプルコードとプロジェクトを提供できます。あなたは迅速で客観的なC実装を好んでいますか? –

+0

甘い、私はAPIに精通しておらず、現在ドキュメントでは紛失しています:) - スウィフトは私にとってはいいです:)ありがとう! –

+0

私は客観的なCプロジェクトをアップロードしますが、質問があれば尋ねてください:) –

答えて

0

私はここからダウンロードすることができますが、あなたは迅速を好む場合、私はまた、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); 
     } 
    } 
} 
関連する問題