ifish/Ifish/controllers/IfishTabControllers/探索/IfishLive/IfishLivelist/ZWCollectionViewFlowLayout.m

98 lines
2.9 KiB
Objective-C
Executable File

//
// ToolBarView.h
// BeautifulChina--test1
//
// Created by scsys on 15/7/20.
// Copyright (c) 2015年 . All rights reserved.
//
#import "ZWCollectionViewFlowLayout.h"
@interface ZWCollectionViewFlowLayout()
@property(nonatomic,retain)NSMutableDictionary * maxYdic;
@end
@implementation ZWCollectionViewFlowLayout
-(NSMutableDictionary *)maxYdic
{
if (!_maxYdic) {
self.maxYdic = [[NSMutableDictionary alloc] init];
}
return _maxYdic;
}
-(instancetype)init
{
if (self=[super init]) {
self.colMagrin = 10;
self.rowMagrin = 10;
self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
self.colCount = 2;
}
return self;
}
-(void)prepareLayout
{
[super prepareLayout];
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
-(CGSize)collectionViewContentSize
{
__block NSString * maxCol = @"0";
//找出最短的列
[self.maxYdic enumerateKeysAndObjectsUsingBlock:^(NSString * column, NSNumber *maxY, BOOL *stop) {
if ([maxY floatValue]>[self.maxYdic[maxCol] floatValue]) {
maxCol = column;
}
}];
return CGSizeMake(0, [self.maxYdic[maxCol] floatValue]);
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
__block NSString * minCol = @"0";
//找出最短的列
[self.maxYdic enumerateKeysAndObjectsUsingBlock:^(NSString * column, NSNumber *maxY, BOOL *stop) {
if ([maxY floatValue]<[self.maxYdic[minCol] floatValue]) {
minCol = column;
}
}];
// 计算宽度
CGFloat width = (self.collectionView.frame.size.width-self.sectionInset.left-self.sectionInset.right-(self.colCount-1)*self.colMagrin)/self.colCount;
// 计算高度
CGFloat hight = [self.degelate ZWwaterFlow:self heightForWidth:width atIndexPath:indexPath];
// NSLog(@"-----%f",width);
// CGFloat hight =100+arc4random_uniform(100);
CGFloat x = self.sectionInset.left + (width+ self.colMagrin)* [minCol intValue];
CGFloat y =[self.maxYdic[minCol] floatValue]+self.rowMagrin;
// 跟新最大的y值
self.maxYdic[minCol] = @(y+hight);
// 计算位置
UICollectionViewLayoutAttributes * attri =[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attri.frame = CGRectMake(x, y, width, hight);
return attri;
}
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
for(int i = 0;i<self.colCount;i++)
{
NSString * col = [NSString stringWithFormat:@"%d",i];
self.maxYdic[col] = @0;
}
NSMutableArray * array = [NSMutableArray array];
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i < count; i++) {
UICollectionViewLayoutAttributes * attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
[array addObject:attrs];
}
return array;
}
@end