首页 > 其他 > 详细

UICollectionViewFlowLayout 瀑布流

时间:2015-12-25 02:12:58      阅读:177      评论:0      收藏:0      [点我收藏+]
?

#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

UICollectionViewFlowLayout 瀑布流

原文:http://zhangmingwei.iteye.com/blog/2266168

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!