11//
22// SeparatorCollectionViewFlowLayout.swift
3+ // ChicByChoice
34//
45// Created by Ivan Bruel on 10/12/15.
6+ // Copyright © 2015 Chic by Choice. All rights reserved.
57//
68
79import Foundation
@@ -21,6 +23,14 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {
2123 }
2224 }
2325
26+ private static let topSeparatorKind = " SeparatorCollectionViewFlowLayout.Top "
27+ private static let bottomSeparatorKind = " SeparatorCollectionViewFlowLayout.Bottom "
28+ private static let leftSeparatorKind = " SeparatorCollectionViewFlowLayout.Left "
29+ private static let rightSeparatorKind = " SeparatorCollectionViewFlowLayout.Right "
30+
31+ private static let separatorKinds = [ leftSeparatorKind, rightSeparatorKind, topSeparatorKind,
32+ bottomSeparatorKind]
33+
2434 init ( separatorWidth: CGFloat = 1 , separatorColor: UIColor = UIColor . blackColor ( ) ) {
2535 self . separatorWidth = separatorWidth
2636 self . separatorColor = separatorColor
@@ -39,13 +49,13 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {
3949 super. prepareLayout ( )
4050
4151 registerClass ( SeparatorView . self,
42- forDecorationViewOfKind: SeparatorKind . topSeparator )
52+ forDecorationViewOfKind: SeparatorCollectionViewFlowLayout . topSeparatorKind )
4353 registerClass ( SeparatorView . self,
44- forDecorationViewOfKind: SeparatorKind . bottomSeparator )
54+ forDecorationViewOfKind: SeparatorCollectionViewFlowLayout . bottomSeparatorKind )
4555 registerClass ( SeparatorView . self,
46- forDecorationViewOfKind: SeparatorKind . rightSeparator )
56+ forDecorationViewOfKind: SeparatorCollectionViewFlowLayout . leftSeparatorKind )
4757 registerClass ( SeparatorView . self,
48- forDecorationViewOfKind: SeparatorKind . leftSeparator )
58+ forDecorationViewOfKind: SeparatorCollectionViewFlowLayout . rightSeparatorKind )
4959 }
5060
5161 override public func layoutAttributesForDecorationViewOfKind( elementKind: String ,
@@ -61,12 +71,27 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {
6171
6272 let baseFrame = cellAttributes. frame
6373
64- guard let transformedFrame = SeparatorKind . init ( rawValue: elementKind) ? . transform ( frame: baseFrame, with: separatorWidth) else {
65- return nil
74+ switch elementKind {
75+ case SeparatorCollectionViewFlowLayout . rightSeparatorKind:
76+ layoutAttributes. frame = CGRect ( x: baseFrame. maxX,
77+ y: baseFrame. minY - separatorWidth, width: separatorWidth,
78+ height: baseFrame. height + separatorWidth * 2 )
79+ case SeparatorCollectionViewFlowLayout . leftSeparatorKind:
80+ layoutAttributes. frame = CGRect ( x: baseFrame. minX - separatorWidth,
81+ y: baseFrame. minY - separatorWidth, width: separatorWidth,
82+ height: baseFrame. height + separatorWidth * 2 )
83+ case SeparatorCollectionViewFlowLayout . topSeparatorKind:
84+ layoutAttributes. frame = CGRect ( x: baseFrame. minX,
85+ y: baseFrame. minY - separatorWidth, width: baseFrame. width,
86+ height: separatorWidth)
87+ case SeparatorCollectionViewFlowLayout . bottomSeparatorKind:
88+ layoutAttributes. frame = CGRect ( x: baseFrame. minX,
89+ y: baseFrame. maxY, width: baseFrame. width,
90+ height: separatorWidth)
91+ default :
92+ break
6693 }
6794
68- layoutAttributes. frame = transformedFrame
69-
7095 layoutAttributes. zIndex = - 1
7196 layoutAttributes. color = separatorColor
7297
@@ -83,9 +108,10 @@ public class SeparatorCollectionViewFlowLayout: UICollectionViewFlowLayout {
83108 baseLayoutAttributes. filter { $0. representedElementCategory == . Cell } . forEach {
84109 ( layoutAttribute) -> ( ) in
85110
86- layoutAttributes += SeparatorKind . elements. flatMap { ( kind) -> UICollectionViewLayoutAttributes ? in
87- layoutAttributesForDecorationViewOfKind ( kind. rawValue, atIndexPath: layoutAttribute. indexPath)
88- }
111+ layoutAttributes += SeparatorCollectionViewFlowLayout . separatorKinds. flatMap {
112+ ( kind) -> UICollectionViewLayoutAttributes ? in
113+ layoutAttributesForDecorationViewOfKind ( kind, atIndexPath: layoutAttribute. indexPath)
114+ }
89115 }
90116
91117 return layoutAttributes
@@ -107,44 +133,7 @@ private class SeparatorView: UICollectionReusableView {
107133 guard let coloredLayoutAttributes = layoutAttributes as? ColoredViewLayoutAttributes else {
108134 return
109135 }
110-
111136 backgroundColor = coloredLayoutAttributes. color
112137 }
113138
114139}
115-
116- private enum SeparatorKind : String {
117- case topSeparator = " SeparatorCollectionViewFlowLayout.Top "
118- case bottomSeparator = " SeparatorCollectionViewFlowLayout.Bottom "
119- case leftSeparator = " SeparatorCollectionViewFlowLayout.Left "
120- case rightSeparator = " SeparatorCollectionViewFlowLayout.Right "
121-
122- static var elements : [ SeparatorKind ] {
123- get {
124- return [ . leftSeparator, . rightSeparator, . topSeparator, . bottomSeparator]
125- }
126- }
127-
128- func transform( frame baseFrame: CGRect , with separatorWidth: CGFloat ) -> CGRect {
129- switch self {
130-
131- case . topSeparator:
132- return CGRect ( x: baseFrame. minX, y: baseFrame. minY - separatorWidth, width: baseFrame. width, height: separatorWidth)
133-
134- case . bottomSeparator:
135- return CGRect ( x: baseFrame. minX, y: baseFrame. maxY, width: baseFrame. width, height: separatorWidth)
136-
137- case . leftSeparator:
138- return CGRect ( x: baseFrame. minX - separatorWidth, y: baseFrame. minY - separatorWidth, width: separatorWidth, height: baseFrame. height + separatorWidth * 2 )
139-
140- case . rightSeparator:
141- return CGRect ( x: baseFrame. maxX, y: baseFrame. minY - separatorWidth, width: separatorWidth, height: baseFrame. height + separatorWidth * 2 )
142- }
143- }
144- }
145-
146- extension UICollectionViewFlowLayout {
147- private func registerClass( viewClass: AnyClass ? , forDecorationViewOfKind elementKind: SeparatorKind ) {
148- registerClass ( viewClass, forDecorationViewOfKind: elementKind. rawValue)
149- }
150- }
0 commit comments