Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save inlinestyle/30018d5629cd84fc3b4a to your computer and use it in GitHub Desktop.
Save inlinestyle/30018d5629cd84fc3b4a to your computer and use it in GitHub Desktop.
class GCFixedColumnHorizontalScrollingCollectionViewFlowLayout: UICollectionViewFlowLayout {
var numberOfRows: UInt = 0
var numberOfFixedColumns: UInt = 0
var columnWidth: UInt = 0
init(numberOfFixedColumns: UInt, columnWidth: UInt) {
self.numberOfFixedColumns = numberOfFixedColumns
self.columnWidth = columnWidth
super.init()
self.scrollDirection = .Horizontal
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let fixedIndexes = numberOfRows * numberOfFixedColumns
var attributesArray = super.layoutAttributesForElementsInRect(rect)?.filter({ (let attributes) -> Bool in
return attributes.indexPath.row >= Int(fixedIndexes)
})
for index in 0..<fixedIndexes {
if let attributes = self.layoutAttributesForItemAtIndexPath(NSIndexPath(forRow: Int(index), inSection: 0)) {
let frame = attributes.frame
let column = index / numberOfRows
attributes.zIndex = 1
attributes.frame = CGRect(x: self.collectionView!.contentOffset.x + CGFloat(columnWidth * column), y: frame.origin.y, width: frame.width, height: frame.height)
attributesArray?.append(attributes)
}
}
return attributesArray
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment