Advertisement

UICollectionView自定义layout

阅读量:

当我们使用Objective-C语言开发iOS应用时 可能会遇到这样的场景:当frame发生变更时相应的cell会随之调整 这种情况既可能因受parent view boundaries的影响而发生 也可能通过复杂且富有创意的方式进行处理 频繁调用reloading数据可能导致性能问题 因此这时我们可以自定义布局并直接编写代码实现

复制代码
 #import "CustomLayout.h"

    
  
    
 #define MainCell @"MainCell"
    
  
    
 @interface CustomLayout ()
    
  
    
 @property (nonatomic, strong) NSMutableDictionary *layoutInfo;
    
  
    
 @end
    
  
    
 @implementation CustomLayout
    
  
    
 - (NSMutableDictionary *)layoutInfo
    
 {
    
     if (!_layoutInfo) {
    
     _layoutInfo = [NSMutableDictionary dictionary];
    
     }
    
  
    
     return _layoutInfo;
    
 }
    
  
    
 - (void)prepareLayout
    
 {
    
     NSMutableDictionary *cellLayoutInfo = [NSMutableDictionary dictionary];
    
  
    
     NSIndexPath *indexPath;
    
  
    
     CGFloat itemWidth;
    
     CGFloat itemSpacing;
    
  
    
     CGFloat widthWithoutSpacing = [self collectionViewContentSize].width / ([self.collectionView numberOfItemsInSection:0]);
    
  
    
     if (widthWithoutSpacing > [self collectionViewContentSize].height) {
    
     itemWidth = [self collectionViewContentSize].height;
    
     itemSpacing = ([self collectionViewContentSize].width - itemWidth*[self.collectionView numberOfItemsInSection:0])/
    
     ([self.collectionView numberOfItemsInSection:0]+1);
    
     }
    
     else {
    
     itemWidth = widthWithoutSpacing;
    
     itemSpacing = 0;
    
     }
    
  
    
     CGFloat xPosition = itemSpacing;
    
  
    
     for (NSInteger section = 0; section < [self.collectionView numberOfSections]; section++) {
    
  
    
     for (NSInteger index = 0 ; index < [self.collectionView numberOfItemsInSection:section] ; index++) {
    
  
    
         indexPath = [NSIndexPath indexPathForItem:index inSection:section];
    
         UICollectionViewLayoutAttributes *itemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
  
    
         CGRect currentFrame=itemAttributes.frame;
    
  
    
         currentFrame.origin.x = xPosition;
    
         currentFrame.size.width = itemWidth;
    
         currentFrame.size.height = itemWidth;
    
  
    
         itemAttributes.frame=currentFrame;
    
         cellLayoutInfo[indexPath] = itemAttributes;
    
  
    
         xPosition += itemWidth + itemSpacing;
    
     }
    
     }
    
  
    
     self.layoutInfo[MainCell] = cellLayoutInfo;
    
 }
    
  
    
 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
    
 {
    
     return YES;
    
 }
    
  
    
 - (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[MainCell][indexPath];
    
 }
    
  
    
  
    
 - (CGSize) collectionViewContentSize
    
 {
    
     return self.collectionView.frame.size;
    
 }
    
  
    
 @end
    
    
    
    
    代码解读

全部评论 (0)

还没有任何评论哟~