Skip to content

Commit baafe31

Browse files
committed
if scrollDirection == .vertical outside the loops
1 parent c1d7dc0 commit baafe31

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

CollectionViewCenteredFlowLayout/CollectionViewCenteredFlowLayout.swift

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Giovanni Lodi
3+
Copyright (c) 2014 Cœur
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

icon.png

4.97 KB
Loading

0 commit comments

Comments
 (0)