IOS简单实现瀑布流UICollectionView

2016-02-19 09:02 84 1 收藏

在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享IOS简单实现瀑布流UICollectionView,希望可以对大家能有小小的帮助。

【 tulaoshi.com - 编程语言 】

UICollectionView 比tableView 灵活,功能也强大很多。系统实现了流式布局,但用处还有很多限制。

要想实现更灵活的布局,就咬重写UICollectionViewLayout。
先看下实现效果:

废话不多说,直接上代码:

先看WaterfallCollectionLayout.m

#import "WaterfallCollectionLayout.h"#define colMargin 5#define colCount 4#define rolMargin 5@interface WaterfallCollectionLayout ()//数组存放每列的总高度@property(nonatomic,strong)NSMutableArray* colsHeight;//单元格宽度@property(nonatomic,assign)CGFloat colWidth;@end

该类要重写以下方法:

//完成布局前的初始工作-(void)prepareLayout;//collectionView的内容尺寸-(CGSize)collectionViewContentSize;//为每个item设置属性-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;//获取制定范围的所有item的属性-(NSArrayUICollectionViewLayoutAttributes * *)layoutAttributesForElementsInRect:(CGRect)rect;-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;

每次调用会清空colsHeight数组里的信息:

//完成布局前的初始工作-(void)prepareLayout{  [super prepareLayout];  self.colWidth =( self.collectionView.frame.size.width - (colCount+1)*colMargin )/colCount;  //让它重新加载  self.colsHeight = nil;}通过遍历colHeight数组里的所有列来获得最长的那一列,返回contentsize//collectionView的内容尺寸-(CGSize)collectionViewContentSize{  NSNumber * longest = self.colsHeight[0];  for (NSInteger i =0;iself.colsHeight.count;i++) {NSNumber* rolHeight = self.colsHeight[i];if(longest.floatValuerolHeight.floatValue){  longest = rolHeight;}  }  return CGSizeMake(self.collectionView.frame.size.width, longest.floatValue);}

每个cell要出来时这个方法会被调用,在此方法中设置该cell的frame。

注意heightBlock是外部控制器传进来的block用以计算每个cell的高度,现在我只是设置了随机数。如果没有传block进来我这里直接让他崩溃了。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)
//为每个item设置属性-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{  UICollectionViewLayoutAttributes* attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];  NSNumber * shortest = self.colsHeight[0];  NSInteger shortCol = 0;  for (NSInteger i =0;iself.colsHeight.count;i++) {NSNumber* rolHeight = self.colsHeight[i];if(shortest.floatValuerolHeight.floatValue){  shortest = rolHeight;  shortCol=i;}  }  CGFloat x = (shortCol+1)*colMargin+ shortCol * self.colWidth;  CGFloat y = shortest.floatValue+colMargin;//获取cell高度  CGFloat height=0;  NSAssert(self.heightBlock!=nil, @"未实现计算高度的block ");  if(self.heightBlock){height = self.heightBlock(indexPath);  }  attr.frame= CGRectMake(x, y, self.colWidth, height);  self.colsHeight[shortCol]=@(shortest.floatValue+colMargin+height);return attr;}
//获取所有item的属性-(NSArrayUICollectionViewLayoutAttributes * *)layoutAttributesForElementsInRect:(CGRect)rect{  NSMutableArray* array = [NSMutableArray array];  NSInteger items = [self.collectionView numberOfItemsInSection:0];  for (int i = 0; iitems;i++) {UICollectionViewLayoutAttributes* attr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];[array addObject:attr];  }  return array;}

实现下列方法会在出现新的cell时重新布局并调用preparelayout方法

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{  return YES;}

每列高度的存放,初始高度可以改,我这里是0

-(NSMutableArray *)colsHeight{  if(!_colsHeight){NSMutableArray * array = [NSMutableArray array];for(int i =0;icolCount;i++){  //这里可以设置初始高度  [array addObject:@(0)];}_colsHeight = [array mutableCopy];  }  return _colsHeight;}

再来看看控制器里就是这么简单

#pragma mark getter-setter-(UICollectionView *)collectionView{  if(!_collectionView){_collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:self.layout];_collectionView.backgroundColor = [UIColor whiteColor];_collectionView.delegate=self;_collectionView.dataSource=self;[_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:identifer];  }  return _collectionView;}-(UICollectionViewLayout *)layout{  if(!_layout){_layout = [[WaterfallCollectionLayout alloc]initWithItemsHeightBlock:^CGFloat(NSIndexPath *index) {  return [self.heightArr[index.item] floatValue];}];  }  return _layout;}-(NSArray *)heightArr{  if(!_heightArr){//随机生成高度NSMutableArray *arr = [NSMutableArray array];for (int i = 0; i100; i++) {  [arr addObject:@(arc4random()%50+80)];}_heightArr = [arr copy];  }  return _heightArr;}

关于瀑布流的文章特别多,本文就是为大家分享了IOS简单实现瀑布流的方法,希望对大家的学习有所帮助。

来源:https://www.tulaoshi.com/n/20160219/1589279.html

延伸阅读
如果你感觉累,那就对了那是因为你在走上坡路。。这句话似乎有点道理的样子,时常提醒自己无论走到哪都不要忘记自己当初为什么出发。有时想想感觉有的东西可以记录一下,就把它记录下来吧,这次想写一下关于单张图片点击全屏预览的问题,网上查了一些大神写的有的功能确实很强大但自己暂时想要的只是简单的功能就好,还有些方法自己也没弄出想...
来源:一起ps吧 作者:不详 本教程更多的是介绍如何溶图。作者用到了三幅素材图片:城堡、瀑布和天空。处理的时候需要把三幅图片完美的融合起来,可以用蒙版等来控制过渡部分。溶图后再渲染一下颜色和高光即可。 最终效果 1、首先打开瀑布图片。 2、扩大画布尺寸,背景白色。 3、拖入城堡图片,位置如下。 4、添加蒙版用黑色笔刷涂抹...
数据流是面向对象程序设计语言中面向对象思想的典型体现,它彻底地取代了早期的利用函数实现输入输出的功能,克服了用函数实现输入输出功能的诸多弊端。简单来说,流就是建立在面向对象基础上的一种抽象的处理数据的工具。在流中,可定义一些处理数据的基本操作,如读取数据、写入数据等,程序员是对流进行操作,而不用关心流的另一头数据...
标签: 周公解梦 解梦
梦见瀑布 梦里瀑布飞流直下,是好事,预示你的职位、社会地位会得到提升,或者财富要增加。 梦里的瀑布平缓流淌,表示生活安逸富贵。 梦见奔腾的瀑布,意味着将有强烈的变化。 小瀑布,兼有女性和男性性特征的意味,冒着泡翻腾的水,让人想起性高潮带来的兴奋。 瀑布在迷雾中,表示心中有一个心愿未了,但经...
1:iostate rdstate ( ) const; 返回的是iostate,简单的说也就是一个int数值.用2进制数据的某一个位置设置为1表示一种状态 2:failbit 的定义如下,二进制数据的第二位为1的时候说明是该数据。 static const _Iostate goodbit = (_Iostate)0x0; static const _Iostate eofbit = (_Iostate)0x1; static const _Iostate failbit = (_Iostate)0x...

经验教程

894

收藏

89
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部