Advertisement
dammarpol

custom collection view

Apr 22nd, 2015
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "CategoryCollectionViewLayout2.h"
  2.  
  3. static NSString * const BHPhotoAlbumLayoutPhotoCellKind = @"Cell";
  4.  
  5. CGFloat spacingX, spacingY;
  6.  
  7. @implementation CategoryCollectionViewLayout2
  8.  
  9. #pragma mark - Lifecycle
  10.  
  11. - (id)init
  12. {
  13.     self = [super init];
  14.     if (self) {
  15.         [self setup];
  16.     }
  17.    
  18.     return self;
  19. }
  20.  
  21. - (id)initWithCoder:(NSCoder *)aDecoder
  22. {
  23.     self = [super init];
  24.     if (self) {
  25.         [self setup];
  26.     }
  27.    
  28.     return self;
  29. }
  30.  
  31. - (void)setup
  32. {
  33.     self.itemInsets = UIEdgeInsetsMake(2.0f, 2.0f, 0.0f, 0.0f);
  34.    
  35.     if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
  36.         self.itemSize = CGSizeMake(160.0f, 160.0f);
  37.     } else {
  38.         self.itemSize = CGSizeMake(70.0f, 70.0f);
  39.     }
  40.    
  41.     self.interItemSpacingY = 2.0f;
  42.    
  43.    UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;
  44.    
  45.     if(iOrientation == 1)
  46.     {
  47.         self.numberOfColumns = 3;
  48.         self.numberOfRows = 4;
  49.     }
  50.     else{
  51.         self.numberOfColumns = 5;
  52.         self.numberOfRows = 3;
  53.     }
  54.  
  55. }
  56.  
  57.  
  58. #pragma mark - Layout
  59.  
  60. - (void)prepareLayout
  61. {
  62.     NSMutableDictionary *newLayoutInfo = [NSMutableDictionary dictionary];
  63.     NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary];
  64.    
  65.     NSInteger sectionCount = [self.collectionView numberOfSections];
  66.     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
  67.    
  68.     for (NSInteger section = 0; section < sectionCount; section++) {
  69.         NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
  70.        
  71.         for (NSInteger item = 0; item < itemCount; item++) {
  72.             indexPath = [NSIndexPath indexPathForItem:item inSection:section];
  73.            
  74.             UICollectionViewLayoutAttributes *itemAttributes =
  75.             [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
  76.             itemAttributes.frame = [self frameForAlbumPhotoAtIndexPath:indexPath];
  77.            
  78.             cellLayoutInfo[indexPath] = itemAttributes;
  79.         }
  80.     }
  81.    
  82.     newLayoutInfo[BHPhotoAlbumLayoutPhotoCellKind] = cellLayoutInfo;
  83.    
  84.     self.layoutInfo = newLayoutInfo;
  85.    
  86. }
  87.  
  88. #pragma mark - Private
  89.  
  90. - (CGRect)frameForAlbumPhotoAtIndexPath:(NSIndexPath *)indexPath
  91. {
  92.     NSInteger row = indexPath.row / self.numberOfColumns;
  93.     NSInteger column = indexPath.row % self.numberOfColumns;
  94.    
  95.     if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
  96.         /*spacingX = self.collectionView.bounds.size.width -
  97.         self.itemInsets.left -
  98.         self.itemInsets.right -
  99.         (self.numberOfColumns * self.itemSize.width);
  100.        
  101.         spacingY = self.collectionView.bounds.size.height - self.itemInsets.top - self.itemInsets.bottom - (self.numberOfRows * self.itemSize.height);*/
  102.        
  103.         spacingX = 7.0f;
  104.        
  105.         spacingY = 7.0f;
  106.        
  107.     } else {
  108.        
  109.         spacingX = 2.0f;
  110.        
  111.         spacingY = 2.0f;
  112.        
  113.     }
  114.    
  115.  
  116.    
  117.     //CGFloat spacingY = 10.0f;
  118.    
  119.     if(self.numberOfRows > 1) spacingY = spacingY / (self.numberOfRows - 1);
  120.    
  121.     //CGFloat spacingX = 0.0f;
  122.    
  123.     if (self.numberOfColumns > 1) spacingX = spacingX / (self.numberOfColumns - 1);
  124.    
  125.     CGFloat originX = floorf(self.itemInsets.left + (self.itemSize.width + spacingX) * column);
  126.    
  127.     CGFloat originY = floor(self.itemInsets.top +
  128.                             (self.itemSize.height + spacingY) * row);
  129.    
  130.     //CGFloat originY = floor((self.itemSize.height + spacingY) * row);
  131.    
  132.     return CGRectMake(originX, originY, self.itemSize.width, self.itemSize.height);
  133.    
  134.    
  135. }
  136.  
  137. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  138. {
  139.    
  140.     NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];
  141.    
  142.     [self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier,
  143.                                                          NSDictionary *elementsInfo,
  144.                                                          BOOL *stop) {
  145.         [elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath,
  146.                                                           UICollectionViewLayoutAttributes *attributes,
  147.                                                           BOOL *innerStop) {
  148.             if (CGRectIntersectsRect(rect, attributes.frame)) {
  149.                 [allAttributes addObject:attributes];
  150.             }
  151.         }];
  152.     }];
  153.    
  154.     return allAttributes;
  155. }
  156.  
  157. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159.     return self.layoutInfo[BHPhotoAlbumLayoutPhotoCellKind][indexPath];
  160. }
  161.  
  162. - (CGSize)collectionViewContentSize
  163. {
  164.     NSInteger rowCount = [self.collectionView numberOfSections] / self.numberOfColumns;
  165.     // make sure we count another row if one is only partially filled
  166.     if ([self.collectionView numberOfSections] % self.numberOfColumns) rowCount++;
  167.    
  168.     CGFloat height = self.itemInsets.top +
  169.     rowCount * self.itemSize.height + (rowCount - 1) * self.interItemSpacingY +
  170.     self.itemInsets.bottom;
  171.    
  172.     return CGSizeMake(self.collectionView.bounds.size.width, height);
  173. }
  174.  
  175. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement