`
zjjzmw1
  • 浏览: 1354002 次
  • 性别: Icon_minigender_1
  • 来自: 开封
社区版块
存档分类
最新评论

UICollectionViewFlowLayout 瀑布流

    博客分类:
  • iOS
阅读更多
 
Tip: 自己开发了好玩的APP: 《小明搜索》(App Store上搜索:"小明搜索")
[点击下载 "小明搜索"](https://itunes.apple.com/cn/app/小明搜索/id1378798470?l=en&mt=8)
1、集必应、百度、搜狗为一体的搜索平台
2、用户随意收藏或添加自己喜欢的网站
3、网页文字播放功能
4、收藏网页的视频,本地播放
5、附加常用小工具
6、后台播放网页音频
7、3D Touch提供便捷入口
完全自定义的搜索助手,简约的爱不释手~
该APP不断完善中~
#import <UIKit/UIKit.h>

@interface CustomFlowLayout : UICollectionViewFlowLayout

@property(nonatomic, assign)IBOutlet id <UICollectionViewDelegateFlowLayout> delegate;
@end
 

#import "CustomFlowLayout.h"

@interface CustomFlowLayout ()<UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)NSMutableArray* attributeArray;

@property(nonatomic, strong)UICollectionViewLayoutAttributes* headerAttributes;
@property(nonatomic, strong)UICollectionViewLayoutAttributes* footerAttribytes;
@end

@implementation CustomFlowLayout


- (void)prepareLayout{
    self.attributeArray = [[NSMutableArray alloc] init];
    [super prepareLayout];
   
    NSInteger numberOfSections = 1;
   
    //行数
    if([self.collectionView.dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]){
        numberOfSections = [self.collectionView.dataSource numberOfSectionsInCollectionView:self.collectionView];
    }
   
   
    CGFloat totalHeight = 0;
    CGFloat itemSpacing = self.minimumInteritemSpacing;
    for (int i = 0; i < numberOfSections; i++) {
       
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:i];

        UIEdgeInsets sectionInset = self.sectionInset;
        if([self.delegate respondsToSelector:@selector(collectionView:layout:insetForSectionAtIndex:)]){
            sectionInset = [self.delegate collectionView:self.collectionView layout:self insetForSectionAtIndex:i];
        }

        CGSize  sectionHeaderSize = self.headerReferenceSize;
        CGSize  sectionFooterSize = self.footerReferenceSize;
       
        if([self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForHeaderInSection:)]){
            sectionHeaderSize = [self.delegate collectionView:self.collectionView layout:self referenceSizeForHeaderInSection:i];
        }
        if([self.delegate respondsToSelector:@selector(collectionView:layout:referenceSizeForFooterInSection:)]){
            sectionFooterSize = [self.delegate collectionView:self.collectionView layout:self referenceSizeForFooterInSection:i];
        }       
       
        CGFloat colHeight[2] = {0, 0};
       
        UICollectionViewLayoutAttributes* attribute;
       
        if(sectionHeaderSize.height > 0){
            attribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:indexPath];
            attribute.frame = CGRectMake(sectionInset.left, sectionInset.top + totalHeight, sectionHeaderSize.width, sectionHeaderSize.height);
            [self.attributeArray addObject:attribute];
        }
       
        if(sectionFooterSize.height > 0){
            attribute = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:indexPath];
            attribute.frame = CGRectMake(sectionInset.left, sectionInset.top, sectionFooterSize.width, sectionFooterSize.height);
            [self.attributeArray addObject:attribute];
        }
       
        totalHeight += sectionHeaderSize.height;
        totalHeight += sectionFooterSize.height;
       
        NSInteger numberOfItemsInSection = 0;
        if([self.collectionView.dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]){
            numberOfItemsInSection = [self.collectionView.dataSource collectionView:self.collectionView numberOfItemsInSection:i];
        }
        for (int j = 0; j < numberOfItemsInSection; j++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
           
            UICollectionViewLayoutAttributes * attris = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
            CGSize size = CGSizeZero;
            NSInteger colum = 0;

            if([self.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]){
                itemSpacing =  [self.delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:i];
            }
           
           
            if([self.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:)]){

                size = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
                if(colHeight[0] <= colHeight[1]){
                    colHeight[0] = colHeight[0] + size.height + itemSpacing;
                    colum = 0;
                }else{
                    colHeight[1] = colHeight[1] + size.height + itemSpacing;
                    colum = 1;
                }
            }
                   
            attris.frame = CGRectMake(sectionInset.left + (itemSpacing + size.width) * colum, colHeight[colum] - size.height - itemSpacing + totalHeight, size.width, size.height);
           
            [self.attributeArray addObject:attris];
           
        }
       
        CGFloat maxHeight = MAX(colHeight[0], colHeight[1]);
       
        totalHeight += (maxHeight -itemSpacing);
    }
       
}

- (CGSize)collectionViewContentSize{
    CGRect frame = [[self.attributeArray lastObject] frame];
    return CGSizeMake(self.collectionView.frame.size.width, frame.origin.y + frame.size.height);
}

- (NSArray<UICollectionViewLayoutAttributes*> *)layoutAttributesForElementsInRect:(CGRect)rect{
   
    return self.attributeArray;
}

- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
    return self.headerAttributes;
}

 

@end
0
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics