@@ -37,46 +37,58 @@ open class CollectionViewCenteredFlowLayout: UICollectionViewFlowLayout {
3737 var representedElements : [ UICollectionViewLayoutAttributes ] = [ ]
3838 var cells : [ [ UICollectionViewLayoutAttributes ] ] = [ [ ] ]
3939 var previousFrame : CGRect ? = nil
40- for layoutAttributes in layoutAttributesForElements {
41- guard layoutAttributes. representedElementKind == nil else {
42- representedElements. append ( layoutAttributes)
43- continue
44- }
45- let currentItemAttributes = layoutAttributes. copy ( ) as! UICollectionViewLayoutAttributes
46- if previousFrame != nil {
47- if scrollDirection == . vertical {
48- // if the current frame, once stretched to the full row intersects the previous frame then they are on the same row
49- if !currentItemAttributes. frame. intersects ( CGRect ( x: - . infinity, y: previousFrame!. origin. y, width: . infinity, height: previousFrame!. size. height) ) {
50- // the item is on a different row
51- cells. append ( [ ] )
52- }
53- } else {
54- // if the current frame, once stretched to the full column intersects the previous frame then they are on the same column
55- if !currentItemAttributes. frame. intersects ( CGRect ( x: previousFrame!. origin. x, y: - . infinity, width: previousFrame!. size. width, height: . infinity) ) {
56- // the item is on a different column
57- cells. append ( [ ] )
58- }
40+ if scrollDirection == . vertical {
41+ for layoutAttributes in layoutAttributesForElements {
42+ guard layoutAttributes. representedElementKind == nil else {
43+ representedElements. append ( layoutAttributes)
44+ continue
5945 }
46+ let currentItemAttributes = layoutAttributes. copy ( ) as! UICollectionViewLayoutAttributes
47+ // if the current frame, once stretched to the full row doesn't intersect the previous frame then they are on different rows
48+ if previousFrame != nil && !currentItemAttributes. frame. intersects ( CGRect ( x: - . infinity, y: previousFrame!. origin. y, width: . infinity, height: previousFrame!. size. height) ) {
49+ cells. append ( [ ] )
50+ }
51+ cells [ cells. endIndex - 1 ] . append ( currentItemAttributes)
52+ previousFrame = currentItemAttributes. frame
6053 }
61- cells [ cells. endIndex - 1 ] . append ( currentItemAttributes)
62- previousFrame = currentItemAttributes. frame
63- }
64- return representedElements + cells. flatMap { group -> [ UICollectionViewLayoutAttributes ] in
65- guard !group. isEmpty else {
66- return group
67- }
68- let section = group. first!. indexPath. section
69- let evaluatedSectionInset = ( collectionView. delegate as? UICollectionViewDelegateFlowLayout ) ? . collectionView ? ( collectionView, layout: self , insetForSectionAt: section) ?? sectionInset
70- let evaluatedMinimumInteritemSpacing = ( collectionView. delegate as? UICollectionViewDelegateFlowLayout ) ? . collectionView ? ( collectionView, layout: self , minimumInteritemSpacingForSectionAt: section) ?? minimumInteritemSpacing
71- if scrollDirection == . vertical {
54+ // we reposition all elements
55+ return representedElements + cells. flatMap { group -> [ UICollectionViewLayoutAttributes ] in
56+ guard let section = group. first? . indexPath. section else {
57+ return group
58+ }
59+ let evaluatedSectionInset = evaluatedSectionInsetForSection ( at: section)
60+ let evaluatedMinimumInteritemSpacing = evaluatedMinimumInteritemSpacingForSection ( at: section)
7261 var origin = ( collectionView. bounds. width + evaluatedSectionInset. left - evaluatedSectionInset. right - group. reduce ( 0 , { $0 + $1. frame. size. width } ) - CGFloat( group. count - 1 ) * evaluatedMinimumInteritemSpacing) / 2
62+ // we reposition each element of a group
7363 return group. map {
7464 $0. frame. origin. x = origin
7565 origin += $0. frame. size. width + evaluatedMinimumInteritemSpacing
7666 return $0
7767 }
78- } else {
68+ }
69+ } else {
70+ for layoutAttributes in layoutAttributesForElements {
71+ guard layoutAttributes. representedElementKind == nil else {
72+ representedElements. append ( layoutAttributes)
73+ continue
74+ }
75+ let currentItemAttributes = layoutAttributes. copy ( ) as! UICollectionViewLayoutAttributes
76+ // if the current frame, once stretched to the full column doesn't intersect the previous frame then they are on different columns
77+ if previousFrame != nil && !currentItemAttributes. frame. intersects ( CGRect ( x: previousFrame!. origin. x, y: - . infinity, width: previousFrame!. size. width, height: . infinity) ) {
78+ cells. append ( [ ] )
79+ }
80+ cells [ cells. endIndex - 1 ] . append ( currentItemAttributes)
81+ previousFrame = currentItemAttributes. frame
82+ }
83+ // we reposition all elements
84+ return representedElements + cells. flatMap { group -> [ UICollectionViewLayoutAttributes ] in
85+ guard let section = group. first? . indexPath. section else {
86+ return group
87+ }
88+ let evaluatedSectionInset = evaluatedSectionInsetForSection ( at: section)
89+ let evaluatedMinimumInteritemSpacing = evaluatedMinimumInteritemSpacingForSection ( at: section)
7990 var origin = ( collectionView. bounds. height + evaluatedSectionInset. top - evaluatedSectionInset. bottom - group. reduce ( 0 , { $0 + $1. frame. size. height } ) - CGFloat( group. count - 1 ) * evaluatedMinimumInteritemSpacing) / 2
91+ // we reposition each element of a group
8092 return group. map {
8193 $0. frame. origin. y = origin
8294 origin += $0. frame. size. height + evaluatedMinimumInteritemSpacing
@@ -86,3 +98,12 @@ open class CollectionViewCenteredFlowLayout: UICollectionViewFlowLayout {
8698 }
8799 }
88100}
101+
102+ extension UICollectionViewFlowLayout {
103+ internal func evaluatedSectionInsetForSection( at section: Int ) -> UIEdgeInsets {
104+ return ( collectionView? . delegate as? UICollectionViewDelegateFlowLayout ) ? . collectionView ? ( collectionView!, layout: self , insetForSectionAt: section) ?? sectionInset
105+ }
106+ internal func evaluatedMinimumInteritemSpacingForSection( at section: Int ) -> CGFloat {
107+ return ( collectionView? . delegate as? UICollectionViewDelegateFlowLayout ) ? . collectionView ? ( collectionView!, layout: self , minimumInteritemSpacingForSectionAt: section) ?? minimumInteritemSpacing
108+ }
109+ }
0 commit comments