Skip to content

Commit

Permalink
- added supporting section headers
Browse files Browse the repository at this point in the history
  • Loading branch information
romansorochak committed Jul 4, 2017
1 parent bb2ee95 commit 32152bd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
36 changes: 32 additions & 4 deletions PinterestLayout/PinterestLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ import UIKit

public protocol CollectionViewLayoutDelegate {

func collectionView(collectionView: UICollectionView, heightForImageAtIndexPath indexPath: IndexPath, withWidth: CGFloat) -> CGFloat
func collectionView(collectionView: UICollectionView,
sizeForSectionHeaderViewForSection section: Int) -> CGSize

func collectionView(collectionView: UICollectionView, heightForAnnotationAtIndexPath indexPath: IndexPath, withWidth: CGFloat) -> CGFloat
func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat

func collectionView(collectionView: UICollectionView,
heightForAnnotationAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat
}


Expand Down Expand Up @@ -98,11 +105,32 @@ public class PinterestLayout: UICollectionViewLayout {
xOffsets.append(CGFloat(collumn) * collumnWidth)
}

var yOffsets = [CGFloat](repeating: 0, count: numberOfColumns)

for section in 0..<numberOfSections {
let numberOfItems = self.numberOfItems(inSection: section)

let headerY = contentHeight
let headerSize = delegate.collectionView(
collectionView: collectionView,
sizeForSectionHeaderViewForSection: section
)
let headerX = (contentWidth - headerSize.width) / 2
let headerFrame = CGRect(
origin: CGPoint(x: headerX, y: headerY),
size: headerSize
)
let attributes = PinterestLayoutAttributes(
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
with: IndexPath(item: 0, section: section)
)
attributes.frame = headerFrame
cache.append(attributes)

contentHeight = headerFrame.maxY
var yOffsets = [CGFloat](
repeating: contentHeight,
count: numberOfColumns
)

for item in 0..<numberOfItems {
let indexPath = IndexPath(item: item, section: section)

Expand Down
18 changes: 15 additions & 3 deletions PinterestLayoutExample/PinterestVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import AVFoundation

class PinterestVC: UICollectionViewController {

var images: [[UIImage]] = [[#imageLiteral(resourceName: "new_york"), #imageLiteral(resourceName: "bigben_river"), #imageLiteral(resourceName: "dubai"), #imageLiteral(resourceName: "4"), #imageLiteral(resourceName: "tiger"), #imageLiteral(resourceName: "5")], [#imageLiteral(resourceName: "adelaide_castle"), #imageLiteral(resourceName: "rys"), #imageLiteral(resourceName: "12"), #imageLiteral(resourceName: "cat"), #imageLiteral(resourceName: "6"), #imageLiteral(resourceName: "city"), #imageLiteral(resourceName: "9"), #imageLiteral(resourceName: "ph"), #imageLiteral(resourceName: "paris"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "bridge"), #imageLiteral(resourceName: "elephants")], [#imageLiteral(resourceName: "venice"), #imageLiteral(resourceName: "bw_new_york"), #imageLiteral(resourceName: "10"), #imageLiteral(resourceName: "kittens"), #imageLiteral(resourceName: "deer"), #imageLiteral(resourceName: "11"), #imageLiteral(resourceName: "8"), #imageLiteral(resourceName: "13"), #imageLiteral(resourceName: "squirell"), #imageLiteral(resourceName: "lion"), #imageLiteral(resourceName: "nature"), #imageLiteral(resourceName: "4")]]

var images: [[UIImage]] = [
[#imageLiteral(resourceName: "new_york"), #imageLiteral(resourceName: "bigben_river"), #imageLiteral(resourceName: "dubai"), #imageLiteral(resourceName: "4"), #imageLiteral(resourceName: "tiger"), #imageLiteral(resourceName: "5")],
[#imageLiteral(resourceName: "adelaide_castle"), #imageLiteral(resourceName: "rys"), #imageLiteral(resourceName: "12"), #imageLiteral(resourceName: "cat"), #imageLiteral(resourceName: "6"), #imageLiteral(resourceName: "city"), #imageLiteral(resourceName: "9"), #imageLiteral(resourceName: "ph"), #imageLiteral(resourceName: "paris"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "bridge"), #imageLiteral(resourceName: "elephants")],
[#imageLiteral(resourceName: "venice"), #imageLiteral(resourceName: "bw_new_york"), #imageLiteral(resourceName: "10"), #imageLiteral(resourceName: "kittens"), #imageLiteral(resourceName: "deer"), #imageLiteral(resourceName: "11"), #imageLiteral(resourceName: "8"), #imageLiteral(resourceName: "13"), #imageLiteral(resourceName: "squirell"), #imageLiteral(resourceName: "lion"), #imageLiteral(resourceName: "nature"), #imageLiteral(resourceName: "4")]
]

override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -46,7 +50,7 @@ class PinterestVC: UICollectionViewController {

//MARK: UICollectionViewDataSource

extension PinterestVC {
extension PinterestVC: UICollectionViewDelegateFlowLayout {

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return images.count
Expand Down Expand Up @@ -78,6 +82,14 @@ extension PinterestVC {

extension PinterestVC: CollectionViewLayoutDelegate {

func collectionView(collectionView: UICollectionView,
sizeForSectionHeaderViewForSection section: Int) -> CGSize {
return CGSize(
width: collectionView.frame.width,
height: 0
)
}

func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat {
Expand Down

0 comments on commit 32152bd

Please sign in to comment.