一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - IOS - IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView

IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView

2021-01-05 12:23tanhui_ui IOS

這篇文章主要為大家介紹了IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

uicollectionview 比tableview 靈活,功能也強(qiáng)大很多。系統(tǒng)實(shí)現(xiàn)了流式布局,但用處還有很多限制。

要想實(shí)現(xiàn)更靈活的布局,就咬重寫(xiě)uicollectionviewlayout。
先看下實(shí)現(xiàn)效果:

IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView

廢話不多說(shuō),直接上代碼:

先看waterfallcollectionlayout.m

?
1
2
3
4
5
6
7
8
9
10
#import "waterfallcollectionlayout.h"
#define colmargin 5
#define colcount 4
#define rolmargin 5
@interface waterfallcollectionlayout ()
//數(shù)組存放每列的總高度
@property(nonatomic,strong)nsmutablearray* colsheight;
//單元格寬度
@property(nonatomic,assign)cgfloat colwidth;
@end

該類要重寫(xiě)以下方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//完成布局前的初始工作
-(void)preparelayout;
 
//collectionview的內(nèi)容尺寸
-(cgsize)collectionviewcontentsize;
 
//為每個(gè)item設(shè)置屬性
-(uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath;
 
//獲取制定范圍的所有item的屬性
-(nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect;
 
-(bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds;

每次調(diào)用會(huì)清空colsheight數(shù)組里的信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//完成布局前的初始工作
-(void)preparelayout{
  [super preparelayout];
  self.colwidth =( self.collectionview.frame.size.width - (colcount+1)*colmargin )/colcount;
  //讓它重新加載
  self.colsheight = nil;
}
通過(guò)遍歷colheight數(shù)組里的所有列來(lái)獲得最長(zhǎng)的那一列,返回contentsize
//collectionview的內(nèi)容尺寸
-(cgsize)collectionviewcontentsize{
  nsnumber * longest = self.colsheight[0];
  for (nsinteger i =0;i<self.colsheight.count;i++) {
    nsnumber* rolheight = self.colsheight[i];
    if(longest.floatvalue<rolheight.floatvalue){
      longest = rolheight;
    }
  }
  return cgsizemake(self.collectionview.frame.size.width, longest.floatvalue);
}

每個(gè)cell要出來(lái)時(shí)這個(gè)方法會(huì)被調(diào)用,在此方法中設(shè)置該cell的frame。

注意heightblock是外部控制器傳進(jìn)來(lái)的block用以計(jì)算每個(gè)cell的高度,現(xiàn)在我只是設(shè)置了隨機(jī)數(shù)。如果沒(méi)有傳block進(jìn)來(lái)我這里直接讓他崩潰了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//為每個(gè)item設(shè)置屬性
-(uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath{
  uicollectionviewlayoutattributes* attr = [uicollectionviewlayoutattributes layoutattributesforcellwithindexpath:indexpath];
  nsnumber * shortest = self.colsheight[0];
  nsinteger shortcol = 0;
  for (nsinteger i =0;i<self.colsheight.count;i++) {
    nsnumber* rolheight = self.colsheight[i];
    if(shortest.floatvalue>rolheight.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, @"未實(shí)現(xiàn)計(jì)算高度的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;
}
?
1
2
3
4
5
6
7
8
9
10
//獲取所有item的屬性
-(nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect{
  nsmutablearray* array = [nsmutablearray array];
  nsinteger items = [self.collectionview numberofitemsinsection:0];
  for (int i = 0; i<items;i++) {
    uicollectionviewlayoutattributes* attr = [self layoutattributesforitematindexpath:[nsindexpath indexpathforitem:i insection:0]];
    [array addobject:attr];
  }
  return array;
}

實(shí)現(xiàn)下列方法會(huì)在出現(xiàn)新的cell時(shí)重新布局并調(diào)用preparelayout方法

?
1
2
3
-(bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds{
  return yes;
}

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

?
1
2
3
4
5
6
7
8
9
10
11
-(nsmutablearray *)colsheight{
  if(!_colsheight){
    nsmutablearray * array = [nsmutablearray array];
    for(int i =0;i<colcount;i++){
      //這里可以設(shè)置初始高度
      [array addobject:@(0)];
    }
    _colsheight = [array mutablecopy];
  }
  return _colsheight;
}

再來(lái)看看控制器里就是這么簡(jiǎn)單

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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){
    //隨機(jī)生成高度
    nsmutablearray *arr = [nsmutablearray array];
    for (int i = 0; i<100; i++) {
      [arr addobject:@(arc4random()%50+80)];
    }
    _heightarr = [arr copy];
  }
  return _heightarr;
}

關(guān)于瀑布流的文章特別多,本文就是為大家分享了ios簡(jiǎn)單實(shí)現(xiàn)瀑布流的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜日本大胆裸艺术 | 羲义嫁密着中出交尾gvg794 | 色就色欧美综合偷拍区a | 亚洲国产精品久久精品成人网站 | 黄色a∨| 色综合视频一区二区三区 | 男人jj视频 | 国产麻豆在线观看网站 | 色一情一区二区三区四区 | 趴好撅高打屁股sp调教h | 午夜福到在线4国产 | 亚洲国产情侣一区二区三区 | 韩国三级大全 | 高h文恩好大好爽 | 学校捏奶揉下面污文h | 羞羞答答免费人成黄页在线观看国产 | 亚洲欧美一 | 校园春色自拍偷拍 | 毛片a级放荡的护士hd | 日本孕妇与黑人xxxxxx | 亚洲AV 无码AV 中文字幕 | 国产精品久久久久久搜索 | 东北恋哥在线播放免费播放 | 青涩体验在线观看未删减 | 动漫美女羞羞 | 色啪啪888.com | 久久免费看少妇高潮A片2012 | 午夜精品亚洲 | 婷射吧 | 高清视频在线观看+免费 | 日本嫩模 | 亚洲欧洲日产国码天堂 | 波多野结衣被绝伦强在线观看 | 韩国三级在线播放 | t66y地址一地址二地址三 | 欧美图片小说 | 欧美夫妇野外交换hd高清版 | 成人欧美一区二区三区 | 精品国产一级在线观看 | 免费标准高清看机机桶机机 | 欧美色精品天天在线观看视频 |