Skip to content

Commit b988720

Browse files
author
Kyle Truscott
committed
Adjust midY coords to accurately group by row, even when heights are variable #4
1 parent b39fde5 commit b988720

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed

Example/CenterFlow.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
5351A7ABCC652379D5412043 /* libPods-CenterFlowTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BA0FF2F233AC4708DDDF959F /* libPods-CenterFlowTests.a */; };
1111
89C9C8C1CAA139E8C3F8DC27 /* libPods-CenterFlow.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19FE6FBDF1A7F0531B0558FC /* libPods-CenterFlow.a */; };
12+
CF0E9DA41AE914A300754C1D /* Launch.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CF0E9DA31AE914A300754C1D /* Launch.storyboard */; };
1213
CF2A543419E702E500BAFA7F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = CF2A543319E702E500BAFA7F /* main.m */; };
1314
CF2A543719E702E500BAFA7F /* KTAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = CF2A543619E702E500BAFA7F /* KTAppDelegate.m */; };
1415
CF2A543F19E702E500BAFA7F /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CF2A543E19E702E500BAFA7F /* Images.xcassets */; };
@@ -35,6 +36,7 @@
3536
53AAACBC5E8ACD76A2F4BF31 /* Pods-CenterFlowTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CenterFlowTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-CenterFlowTests/Pods-CenterFlowTests.release.xcconfig"; sourceTree = "<group>"; };
3637
A75292300854AF4BFB32B96E /* Pods-CenterFlow.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CenterFlow.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CenterFlow/Pods-CenterFlow.debug.xcconfig"; sourceTree = "<group>"; };
3738
BA0FF2F233AC4708DDDF959F /* libPods-CenterFlowTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-CenterFlowTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
39+
CF0E9DA31AE914A300754C1D /* Launch.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Launch.storyboard; sourceTree = "<group>"; };
3840
CF2A542E19E702E500BAFA7F /* CenterFlow.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CenterFlow.app; sourceTree = BUILT_PRODUCTS_DIR; };
3941
CF2A543219E702E500BAFA7F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4042
CF2A543319E702E500BAFA7F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -123,6 +125,7 @@
123125
CF2A545819E704C700BAFA7F /* KTMainController.m */,
124126
CF2A545A19E7061300BAFA7F /* KTAwesomeCell.h */,
125127
CF2A545B19E7061300BAFA7F /* KTAwesomeCell.m */,
128+
CF0E9DA31AE914A300754C1D /* Launch.storyboard */,
126129
);
127130
path = CenterFlow;
128131
sourceTree = "<group>";
@@ -239,6 +242,7 @@
239242
files = (
240243
CF2A543F19E702E500BAFA7F /* Images.xcassets in Resources */,
241244
CF2A545E19E7084100BAFA7F /* Default-568h@2x.png in Resources */,
245+
CF0E9DA41AE914A300754C1D /* Launch.storyboard in Resources */,
242246
);
243247
runOnlyForDeploymentPostprocessing = 0;
244248
};

Example/CenterFlow/Info.plist

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<string>1</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
25+
<key>UILaunchStoryboardName</key>
26+
<string>Launch</string>
2527
<key>UIRequiredDeviceCapabilities</key>
2628
<array>
2729
<string>armv7</string>

Example/CenterFlow/KTMainController.m

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#import "KTCenterFlowLayout.h"
1111
#import "KTAwesomeCell.h"
1212

13+
#define VARY_HEIGHTS YES
14+
1315
@interface KTMainController ()
1416
@property (strong) NSArray *states;
1517
@property (strong) KTAwesomeCell *sizingCell;
@@ -120,6 +122,13 @@ - (CGSize)collectionView:(UICollectionView *)collectionView
120122
self.sizingCell.label.text = self.states[indexPath.row];
121123
[self.sizingCell setNeedsLayout];
122124
[self.sizingCell layoutIfNeeded];
125+
#ifdef VARY_HEIGHTS
126+
CGSize size = self.sizingCell.intrinsicContentSize;
127+
size.height = (self.sizingCell.label.text.length == 7) ? size.height+20 : size.height;
128+
size.height = (self.sizingCell.label.text.length == 5) ? size.height+25 : size.height;
129+
size.height = (self.sizingCell.label.text.length == 8) ? size.height+45 : size.height;
130+
return size;
131+
#endif
123132
return self.sizingCell.intrinsicContentSize;
124133
}
125134

Example/CenterFlow/Launch.storyboard

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7531" systemVersion="14C109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
3+
<dependencies>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
6+
</dependencies>
7+
<scenes>
8+
<!--View Controller-->
9+
<scene sceneID="4LV-eu-Rhf">
10+
<objects>
11+
<viewController id="cNG-2Q-PTa" sceneMemberID="viewController">
12+
<layoutGuides>
13+
<viewControllerLayoutGuide type="top" id="SmG-85-xYz"/>
14+
<viewControllerLayoutGuide type="bottom" id="va6-mK-Fwz"/>
15+
</layoutGuides>
16+
<view key="view" contentMode="scaleToFill" id="HWO-Mg-wu0">
17+
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
18+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19+
<subviews>
20+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="KTCenterFlowLayout" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bwM-3B-q6y">
21+
<rect key="frame" x="219" y="294" width="162" height="21"/>
22+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
23+
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
24+
<nil key="highlightedColor"/>
25+
</label>
26+
</subviews>
27+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
28+
<constraints>
29+
<constraint firstAttribute="centerX" secondItem="bwM-3B-q6y" secondAttribute="centerX" id="NZc-3P-4QE"/>
30+
<constraint firstAttribute="centerY" secondItem="bwM-3B-q6y" secondAttribute="centerY" id="PIn-4W-T3e"/>
31+
</constraints>
32+
</view>
33+
</viewController>
34+
<placeholder placeholderIdentifier="IBFirstResponder" id="tZf-8z-bJe" userLabel="First Responder" sceneMemberID="firstResponder"/>
35+
</objects>
36+
<point key="canvasLocation" x="312" y="210"/>
37+
</scene>
38+
</scenes>
39+
</document>

KTCenterFlowLayout.m

+27-8
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,49 @@
77

88
#import "KTCenterFlowLayout.h"
99

10+
@interface KTCenterFlowLayout ()
11+
@end
12+
1013
@implementation KTCenterFlowLayout
1114

1215
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
1316
{
1417
NSMutableArray *superAttributes = [NSMutableArray arrayWithArray:[super layoutAttributesForElementsInRect:rect]];
15-
18+
1619
NSMutableDictionary *rowCollections = [NSMutableDictionary new];
1720

1821
// Collect attributes by their midY coordinate.. i.e. rows!
22+
1923
for (UICollectionViewLayoutAttributes *itemAttributes in superAttributes)
2024
{
21-
NSNumber *yCenter = @(CGRectGetMidY(itemAttributes.frame));
22-
23-
if (!rowCollections[yCenter])
24-
rowCollections[yCenter] = [NSMutableArray new];
25-
26-
[rowCollections[yCenter] addObject:itemAttributes];
25+
// Normalize the midY to others in the row
26+
// with variable cell heights the midYs can be ever so slightly
27+
// different.
28+
CGFloat midYRound = roundf(CGRectGetMidY(itemAttributes.frame));
29+
CGFloat midYPlus = midYRound + 1;
30+
CGFloat midYMinus = midYRound - 1;
31+
NSNumber *key;
32+
33+
if (rowCollections[@(midYPlus)])
34+
key = @(midYPlus);
35+
36+
if (rowCollections[@(midYMinus)])
37+
key = @(midYMinus);
38+
39+
if (!key)
40+
key = @(midYRound);
41+
42+
if (!rowCollections[key])
43+
rowCollections[key] = [NSMutableArray new];
44+
45+
[(NSMutableArray *) rowCollections[key] addObject:itemAttributes];
2746
}
2847

2948
CGFloat collectionViewWidth = CGRectGetWidth(self.collectionView.bounds);
3049

3150
// Adjust the items in each row
3251
[rowCollections enumerateKeysAndObjectsUsingBlock:^(id key, NSArray *itemAttributesCollection, BOOL *stop) {
33-
52+
3453
NSInteger itemsInRow = [itemAttributesCollection count];
3554

3655
// x-x-x-x ... sum up the interim space

0 commit comments

Comments
 (0)