Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make more pixel-perfect #83

Merged
merged 2 commits into from
Jan 14, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions CHTCollectionViewWaterfallLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import "CHTCollectionViewWaterfallLayout.h"
#import "tgmath.h"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is it for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


NSString *const CHTCollectionElementKindSectionHeader = @"CHTCollectionElementKindSectionHeader";
NSString *const CHTCollectionElementKindSectionFooter = @"CHTCollectionElementKindSectionFooter";
Expand All @@ -30,7 +31,12 @@ @interface CHTCollectionViewWaterfallLayout ()
@implementation CHTCollectionViewWaterfallLayout

/// How many items to be union into a single rectangle
const NSInteger unionSize = 20;
static const NSInteger unionSize = 20;

static CGFloat CHTFloorCGFloat(CGFloat value) {
CGFloat scale = [UIScreen mainScreen].scale;
return floor(value * scale) / scale;
}

#pragma mark - Public Accessors
- (void)setColumnCount:(NSInteger)columnCount {
Expand Down Expand Up @@ -113,7 +119,7 @@ - (CGFloat)itemWidthInSectionAtIndex:(NSInteger)section {
}
CGFloat width = self.collectionView.frame.size.width - sectionInset.left - sectionInset.right;
NSInteger columnCount = [self columnCountForSection:section];
return floorf((width - (columnCount - 1) * self.minimumColumnSpacing) / columnCount);
return CHTFloorCGFloat((width - (columnCount - 1) * self.minimumColumnSpacing) / columnCount);
}

#pragma mark - Private Accessors
Expand Down Expand Up @@ -244,7 +250,7 @@ - (void)prepareLayout {

CGFloat width = self.collectionView.frame.size.width - sectionInset.left - sectionInset.right;
NSInteger columnCount = [self columnCountForSection:section];
CGFloat itemWidth = floorf((width - (columnCount - 1) * self.minimumColumnSpacing) / columnCount);
CGFloat itemWidth = CHTFloorCGFloat((width - (columnCount - 1) * self.minimumColumnSpacing) / columnCount);

/*
* 2. Section header
Expand Down Expand Up @@ -298,7 +304,7 @@ - (void)prepareLayout {
CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
CGFloat itemHeight = 0;
if (itemSize.height > 0 && itemSize.width > 0) {
itemHeight = floorf(itemSize.height * itemWidth / itemSize.width);
itemHeight = CHTFloorCGFloat(itemSize.height * itemWidth / itemSize.width);
}

attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
Expand Down