Created
October 17, 2015 06:56
-
-
Save xdream86/10fb6a87d7622fc2f543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface MFLMHorizontalFlowLayout() | |
@property (nonatomic, strong) NSDictionary *layoutInfo; | |
@end | |
@implementation MFLMHorizontalFlowLayout | |
- (void)prepareLayout { | |
self.itemSize = CGSizeMake((ScreenRect.size.width - 45) / 3, (ScreenRect.size.width - 45) / 3); | |
NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary]; | |
NSInteger sectionCount = [self.collectionView numberOfSections]; | |
for (int section = 0; section < sectionCount; section++) { | |
for (int item = 0; item < [self.collectionView numberOfItemsInSection:section]; item++) { | |
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section]; | |
UICollectionViewLayoutAttributes *itemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; | |
itemAttributes.frame = [self frameForAlbumPhotoAtIndexPath:indexPath]; | |
cellLayoutInfo[indexPath] = itemAttributes; | |
} | |
} | |
self.layoutInfo = cellLayoutInfo; | |
} | |
- (CGSize)collectionViewContentSize { | |
CGSize boundsSize = self.collectionView.bounds.size; | |
NSInteger numberOfPages = [self.collectionView numberOfSections]; | |
CGSize size = CGSizeMake(boundsSize.width * numberOfPages, boundsSize.height); | |
return size; | |
} | |
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { | |
NSMutableArray *allAttributes = [NSMutableArray new]; | |
[self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *innerStop) { | |
if (CGRectIntersectsRect(rect, attributes.frame)) { | |
[allAttributes addObject:attributes]; | |
} | |
}]; | |
return allAttributes; | |
} | |
- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { | |
return self.layoutInfo[indexPath]; | |
} | |
- (CGRect)frameForAlbumPhotoAtIndexPath:(NSIndexPath *)indexPath { | |
CGSize itemSize = self.itemSize; | |
if (itemSize.width == 0 || itemSize.height == 0) { | |
return CGRectZero; | |
} | |
NSInteger item = indexPath.item; | |
CGRect bounds = self.collectionView.bounds; | |
NSInteger rowPosition = (item / 3) % 3; | |
NSInteger columnPosition = item % 3; | |
CGRect frame = CGRectZero; | |
frame.origin.x = indexPath.section * bounds.size.width + 15 + columnPosition * itemSize.width + (15 / 2) * columnPosition; | |
frame.origin.y = rowPosition * itemSize.height + 15 + (15 / 2) * rowPosition; | |
frame.size = self.itemSize; | |
return frame; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment