Skip to content

Commit

Permalink
- refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
romansorochak committed Jul 4, 2017
1 parent be746a2 commit bb2ee95
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
12 changes: 10 additions & 2 deletions PinterestLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@
path = ../PinterestLayout;
sourceTree = "<group>";
};
B572E5ED1F0C0D5500540F7C /* Supporting Files */ = {
isa = PBXGroup;
children = (
B5CF9EE91F0BB19500DAA3B7 /* Info.plist */,
);
name = "Supporting Files";
sourceTree = "<group>";
};
B5AFCB9D1F0BB16F00CB71B5 = {
isa = PBXGroup;
children = (
Expand All @@ -129,8 +137,8 @@
isa = PBXGroup;
children = (
B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */,
B5AFCBAB1F0BB16F00CB71B5 /* Info.plist */,
B5CF9EF21F0BB1D200DAA3B7 /* PinterestLayout.swift */,
B5AFCBAB1F0BB16F00CB71B5 /* Info.plist */,
);
path = PinterestLayout;
sourceTree = "<group>";
Expand All @@ -142,7 +150,7 @@
B5CF9EDF1F0BB19500DAA3B7 /* PinterestVC.swift */,
640095F41F0BC340006AE391 /* UI */,
640095F81F0BC3E8006AE391 /* Recourses */,
B5CF9EE91F0BB19500DAA3B7 /* Info.plist */,
B572E5ED1F0C0D5500540F7C /* Supporting Files */,
);
path = PinterestLayoutExample;
sourceTree = "<group>";
Expand Down
78 changes: 48 additions & 30 deletions PinterestLayout/PinterestLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ import UIKit

public protocol CollectionViewLayoutDelegate {

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

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


//MARK: CollectionViewLayoutAttributes

public class CollectionViewLayoutAttributes: UICollectionViewLayoutAttributes {
public class PinterestLayoutAttributes: UICollectionViewLayoutAttributes {

public var imageHeight: CGFloat = 0


override public func copy(with zone: NSZone? = nil) -> Any {
let copy = super.copy(with: zone) as! CollectionViewLayoutAttributes
let copy = super.copy(with: zone) as! PinterestLayoutAttributes
copy.imageHeight = imageHeight
return copy
}

override public func isEqual(_ object: Any?) -> Bool {
if let attributes = object as? CollectionViewLayoutAttributes {
if let attributes = object as? PinterestLayoutAttributes {
if attributes.imageHeight == imageHeight {
return super.isEqual(object)
}
Expand All @@ -49,33 +50,46 @@ public class PinterestLayout: UICollectionViewLayout {
public var delegate: CollectionViewLayoutDelegate!
public var numberOfColumns: Int = 1
public var cellPadding: CGFloat = 0
public var numberOfSections: Int = 1
var insets = UIEdgeInsets.zero

private var cache = [CollectionViewLayoutAttributes]()
private var cache = [PinterestLayoutAttributes]()
private var contentHeight: CGFloat = 0
private var width: CGFloat {
private var contentWidth: CGFloat {
get {
insets = collectionView!.contentInset
let bounds = collectionView!.bounds
let bounds = collectionView.bounds
let insets = collectionView.contentInset
return bounds.width - insets.left - insets.right
}
}

override public var collectionViewContentSize: CGSize {
get {
return CGSize(width: width, height: contentHeight)
return CGSize(
width: contentWidth,
height: contentHeight
)
}
}

override public class var layoutAttributesClass: AnyClass {
return CollectionViewLayoutAttributes.self
return PinterestLayoutAttributes.self
}

override public var collectionView: UICollectionView {
return super.collectionView!
}

var numberOfSections: Int {
return collectionView.numberOfSections
}

func numberOfItems(inSection section: Int) -> Int {
return collectionView.numberOfItems(inSection: section)
}


override public func prepare() {

if cache.isEmpty {
let collumnWidth = width / CGFloat(numberOfColumns)
let collumnWidth = contentWidth / CGFloat(numberOfColumns)
let cellWidth = collumnWidth - (cellPadding * 2)

var xOffsets = [CGFloat]()
Expand All @@ -85,39 +99,43 @@ public class PinterestLayout: UICollectionViewLayout {
}

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

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

var column = 0
var oneColumn = Int(collectionView!.numberOfItems(inSection: section)/numberOfColumns)
if collectionView!.numberOfItems(inSection: section) % numberOfColumns != 0 {
oneColumn += 1
}
for item in 0..<collectionView!.numberOfItems(inSection: section) {

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

column = Int(item/(oneColumn))
let column = yOffsets.index(of: yOffsets.min() ?? 0) ?? 0

let imageHeight = delegate.collectionView(collectionView: collectionView!, heightForImageAtIndexPath: indexPath as NSIndexPath, withWidth: cellWidth)
let annotationHeight = delegate.collectionView(collectionView: collectionView!, heightForAnnotationAtIndexPath: indexPath as NSIndexPath, withWidth: cellWidth)
let imageHeight = delegate.collectionView(
collectionView: collectionView,
heightForImageAtIndexPath: indexPath,
withWidth: cellWidth
)
let annotationHeight = delegate.collectionView(
collectionView: collectionView,
heightForAnnotationAtIndexPath: indexPath,
withWidth: cellWidth
)
let cellHeight = cellPadding + imageHeight + annotationHeight + cellPadding

let frame = CGRect(
x: xOffsets[column],
y: yOffsets[column],
width: collumnWidth,
height: cellHeight)
let insetFrame = UIEdgeInsetsInsetRect(frame, insets)
height: cellHeight
)

let attributes = CollectionViewLayoutAttributes(
let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
let attributes = PinterestLayoutAttributes(
forCellWith: indexPath
)
attributes.frame = insetFrame
attributes.imageHeight = imageHeight
cache.append(attributes)

let cgrectFrame = frame
contentHeight = max(contentHeight, cgrectFrame.maxY)
contentHeight = max(contentHeight, frame.maxY)
yOffsets[column] = yOffsets[column] + cellHeight
}
}
Expand Down
2 changes: 1 addition & 1 deletion PinterestLayoutExample/CollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CollectionViewCell: UICollectionViewCell {

override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
let attributes = layoutAttributes as! CollectionViewLayoutAttributes
let attributes = layoutAttributes as! PinterestLayoutAttributes
imageViewHeightLayoutConstraint.constant = attributes.imageHeight
}
}
18 changes: 13 additions & 5 deletions PinterestLayoutExample/PinterestVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ class PinterestVC: UICollectionViewController {

private func setupCollectionViewInsets() {
collectionView!.backgroundColor = .clear
collectionView!.contentInset = UIEdgeInsets(top: 15, left: 5, bottom: 5, right: 5)
collectionView!.contentInset = UIEdgeInsets(
top: 15,
left: 5,
bottom: 5,
right: 5
)
}

private func setupLayout() {
guard let layout = collectionViewLayout as? PinterestLayout else { return }
layout.delegate = self
layout.cellPadding = 5
layout.numberOfColumns = 2
layout.numberOfSections = images.count
}
}

Expand Down Expand Up @@ -73,15 +77,19 @@ extension PinterestVC {
//MARK: CollectionViewLayoutDelegate

extension PinterestVC: CollectionViewLayoutDelegate {
func collectionView(collectionView: UICollectionView, heightForImageAtIndexPath indexPath: NSIndexPath, withWidth: CGFloat) -> CGFloat {


func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat {
let image = images[indexPath.section][indexPath.item]
let boundingRect = CGRect(x: 0, y: 0, width: withWidth, height: CGFloat(MAXFLOAT))
let rect = AVMakeRect(aspectRatio: image.size, insideRect: boundingRect)
return rect.height
}

func collectionView(collectionView: UICollectionView, heightForAnnotationAtIndexPath indexPath: NSIndexPath, withWidth: CGFloat) -> CGFloat {
func collectionView(collectionView: UICollectionView,
heightForAnnotationAtIndexPath indexPath: IndexPath,
withWidth: CGFloat) -> CGFloat {
return 50
}
}

0 comments on commit bb2ee95

Please sign in to comment.