Skip to content

Commit

Permalink
Merged conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanbruel committed Jan 7, 2016
1 parent c404a65 commit 61854bd
Showing 1 changed file with 37 additions and 48 deletions.
85 changes: 37 additions & 48 deletions Source/SeparatorCollectionViewFlowLayout.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//
// SeparatorCollectionViewFlowLayout.swift
// ChicByChoice
//
// Created by Ivan Bruel on 10/12/15.
// Copyright © 2015 Chic by Choice. All rights reserved.
//

import Foundation
Expand All @@ -21,6 +23,14 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {
}
}

private static let topSeparatorKind = "SeparatorCollectionViewFlowLayout.Top"
private static let bottomSeparatorKind = "SeparatorCollectionViewFlowLayout.Bottom"
private static let leftSeparatorKind = "SeparatorCollectionViewFlowLayout.Left"
private static let rightSeparatorKind = "SeparatorCollectionViewFlowLayout.Right"

private static let separatorKinds = [leftSeparatorKind, rightSeparatorKind, topSeparatorKind,
bottomSeparatorKind]

init(separatorWidth: CGFloat = 1, separatorColor: UIColor = UIColor.blackColor()) {
self.separatorWidth = separatorWidth
self.separatorColor = separatorColor
Expand All @@ -39,13 +49,13 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {
super.prepareLayout()

registerClass(SeparatorView.self,
forDecorationViewOfKind: SeparatorKind.topSeparator)
forDecorationViewOfKind: SeparatorCollectionViewFlowLayout.topSeparatorKind)
registerClass(SeparatorView.self,
forDecorationViewOfKind: SeparatorKind.bottomSeparator)
forDecorationViewOfKind: SeparatorCollectionViewFlowLayout.bottomSeparatorKind)
registerClass(SeparatorView.self,
forDecorationViewOfKind: SeparatorKind.rightSeparator)
forDecorationViewOfKind: SeparatorCollectionViewFlowLayout.leftSeparatorKind)
registerClass(SeparatorView.self,
forDecorationViewOfKind: SeparatorKind.leftSeparator)
forDecorationViewOfKind: SeparatorCollectionViewFlowLayout.rightSeparatorKind)
}

override public func layoutAttributesForDecorationViewOfKind(elementKind: String,
Expand All @@ -61,12 +71,27 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {

let baseFrame = cellAttributes.frame

guard let transformedFrame = SeparatorKind.init(rawValue: elementKind)?.transform(frame: baseFrame, with: separatorWidth) else {
return nil
switch elementKind {
case SeparatorCollectionViewFlowLayout.rightSeparatorKind:
layoutAttributes.frame = CGRect(x: baseFrame.maxX,
y: baseFrame.minY - separatorWidth, width: separatorWidth,
height: baseFrame.height + separatorWidth * 2)
case SeparatorCollectionViewFlowLayout.leftSeparatorKind:
layoutAttributes.frame = CGRect(x: baseFrame.minX - separatorWidth,
y: baseFrame.minY - separatorWidth, width: separatorWidth,
height: baseFrame.height + separatorWidth * 2)
case SeparatorCollectionViewFlowLayout.topSeparatorKind:
layoutAttributes.frame = CGRect(x: baseFrame.minX,
y: baseFrame.minY - separatorWidth, width: baseFrame.width,
height: separatorWidth)
case SeparatorCollectionViewFlowLayout.bottomSeparatorKind:
layoutAttributes.frame = CGRect(x: baseFrame.minX,
y: baseFrame.maxY, width: baseFrame.width,
height: separatorWidth)
default:
break
}

layoutAttributes.frame = transformedFrame

layoutAttributes.zIndex = -1
layoutAttributes.color = separatorColor

Expand All @@ -83,9 +108,10 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {
baseLayoutAttributes.filter { $0.representedElementCategory == .Cell }.forEach {
(layoutAttribute) -> () in

layoutAttributes += SeparatorKind.elements.flatMap { (kind) -> UICollectionViewLayoutAttributes? in
layoutAttributesForDecorationViewOfKind(kind.rawValue, atIndexPath: layoutAttribute.indexPath)
}
layoutAttributes += SeparatorCollectionViewFlowLayout.separatorKinds.flatMap {
(kind) -> UICollectionViewLayoutAttributes? in
layoutAttributesForDecorationViewOfKind(kind, atIndexPath: layoutAttribute.indexPath)
}
}

return layoutAttributes
Expand All @@ -107,44 +133,7 @@ private class SeparatorView: UICollectionReusableView {
guard let coloredLayoutAttributes = layoutAttributes as? ColoredViewLayoutAttributes else {
return
}

backgroundColor = coloredLayoutAttributes.color
}

}

private enum SeparatorKind: String {
case topSeparator = "SeparatorCollectionViewFlowLayout.Top"
case bottomSeparator = "SeparatorCollectionViewFlowLayout.Bottom"
case leftSeparator = "SeparatorCollectionViewFlowLayout.Left"
case rightSeparator = "SeparatorCollectionViewFlowLayout.Right"

static var elements: [SeparatorKind] {
get {
return [.leftSeparator, .rightSeparator, .topSeparator, .bottomSeparator]
}
}

func transform(frame baseFrame: CGRect, with separatorWidth: CGFloat) -> CGRect {
switch self {

case .topSeparator:
return CGRect(x: baseFrame.minX, y: baseFrame.minY - separatorWidth, width: baseFrame.width, height: separatorWidth)

case .bottomSeparator:
return CGRect(x: baseFrame.minX, y: baseFrame.maxY, width: baseFrame.width, height: separatorWidth)

case .leftSeparator:
return CGRect(x: baseFrame.minX - separatorWidth, y: baseFrame.minY - separatorWidth, width: separatorWidth, height: baseFrame.height + separatorWidth * 2)

case .rightSeparator:
return CGRect(x: baseFrame.maxX, y: baseFrame.minY - separatorWidth, width: separatorWidth, height: baseFrame.height + separatorWidth * 2)
}
}
}

extension UICollectionViewFlowLayout {
private func registerClass(viewClass: AnyClass?, forDecorationViewOfKind elementKind: SeparatorKind) {
registerClass(viewClass, forDecorationViewOfKind: elementKind.rawValue)
}
}

0 comments on commit 61854bd

Please sign in to comment.