Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "CategoryCollectionViewLayout2.h"
- static NSString * const BHPhotoAlbumLayoutPhotoCellKind = @"Cell";
- CGFloat spacingX, spacingY;
- @implementation CategoryCollectionViewLayout2
- #pragma mark - Lifecycle
- - (id)init
- {
- self = [super init];
- if (self) {
- [self setup];
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)aDecoder
- {
- self = [super init];
- if (self) {
- [self setup];
- }
- return self;
- }
- - (void)setup
- {
- self.itemInsets = UIEdgeInsetsMake(2.0f, 2.0f, 0.0f, 0.0f);
- if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
- self.itemSize = CGSizeMake(160.0f, 160.0f);
- } else {
- self.itemSize = CGSizeMake(70.0f, 70.0f);
- }
- self.interItemSpacingY = 2.0f;
- UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;
- if(iOrientation == 1)
- {
- self.numberOfColumns = 3;
- self.numberOfRows = 4;
- }
- else{
- self.numberOfColumns = 5;
- self.numberOfRows = 3;
- }
- }
- #pragma mark - Layout
- - (void)prepareLayout
- {
- NSMutableDictionary *newLayoutInfo = [NSMutableDictionary dictionary];
- NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary];
- NSInteger sectionCount = [self.collectionView numberOfSections];
- NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
- for (NSInteger section = 0; section < sectionCount; section++) {
- NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
- for (NSInteger item = 0; item < itemCount; item++) {
- indexPath = [NSIndexPath indexPathForItem:item inSection:section];
- UICollectionViewLayoutAttributes *itemAttributes =
- [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
- itemAttributes.frame = [self frameForAlbumPhotoAtIndexPath:indexPath];
- cellLayoutInfo[indexPath] = itemAttributes;
- }
- }
- newLayoutInfo[BHPhotoAlbumLayoutPhotoCellKind] = cellLayoutInfo;
- self.layoutInfo = newLayoutInfo;
- }
- #pragma mark - Private
- - (CGRect)frameForAlbumPhotoAtIndexPath:(NSIndexPath *)indexPath
- {
- NSInteger row = indexPath.row / self.numberOfColumns;
- NSInteger column = indexPath.row % self.numberOfColumns;
- if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
- /*spacingX = self.collectionView.bounds.size.width -
- self.itemInsets.left -
- self.itemInsets.right -
- (self.numberOfColumns * self.itemSize.width);
- spacingY = self.collectionView.bounds.size.height - self.itemInsets.top - self.itemInsets.bottom - (self.numberOfRows * self.itemSize.height);*/
- spacingX = 7.0f;
- spacingY = 7.0f;
- } else {
- spacingX = 2.0f;
- spacingY = 2.0f;
- }
- //CGFloat spacingY = 10.0f;
- if(self.numberOfRows > 1) spacingY = spacingY / (self.numberOfRows - 1);
- //CGFloat spacingX = 0.0f;
- if (self.numberOfColumns > 1) spacingX = spacingX / (self.numberOfColumns - 1);
- CGFloat originX = floorf(self.itemInsets.left + (self.itemSize.width + spacingX) * column);
- CGFloat originY = floor(self.itemInsets.top +
- (self.itemSize.height + spacingY) * row);
- //CGFloat originY = floor((self.itemSize.height + spacingY) * row);
- return CGRectMake(originX, originY, self.itemSize.width, self.itemSize.height);
- }
- - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
- {
- NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];
- [self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier,
- NSDictionary *elementsInfo,
- BOOL *stop) {
- [elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath,
- UICollectionViewLayoutAttributes *attributes,
- BOOL *innerStop) {
- if (CGRectIntersectsRect(rect, attributes.frame)) {
- [allAttributes addObject:attributes];
- }
- }];
- }];
- return allAttributes;
- }
- - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- return self.layoutInfo[BHPhotoAlbumLayoutPhotoCellKind][indexPath];
- }
- - (CGSize)collectionViewContentSize
- {
- NSInteger rowCount = [self.collectionView numberOfSections] / self.numberOfColumns;
- // make sure we count another row if one is only partially filled
- if ([self.collectionView numberOfSections] % self.numberOfColumns) rowCount++;
- CGFloat height = self.itemInsets.top +
- rowCount * self.itemSize.height + (rowCount - 1) * self.interItemSpacingY +
- self.itemInsets.bottom;
- return CGSizeMake(self.collectionView.bounds.size.width, height);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement