最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年04月20日 11:30:10   作者:tanhui_ui  
這篇文章主要為大家介紹了IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

先看WaterfallCollectionLayout.m

#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

該類要重寫以下方法:

//完成布局前的初始工作
-(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ù)組里的信息:

//完成布局前的初始工作
-(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)我這里直接讓他崩潰了。

//為每個(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;
}
//獲取所有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方法

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
 return YES;
}

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

-(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)單

#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í)有所幫助。

相關(guān)文章

  • iOS開發(fā)中Quartz2D繪圖路徑的使用以及條紋效果的實(shí)現(xiàn)

    iOS開發(fā)中Quartz2D繪圖路徑的使用以及條紋效果的實(shí)現(xiàn)

    這篇文章主要介紹了iOS開發(fā)中Quartz2D繪圖路徑的使用以及條紋效果的實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • iOS中sqlite數(shù)據(jù)庫(kù)的原生用法

    iOS中sqlite數(shù)據(jù)庫(kù)的原生用法

    這篇文章主要為大家詳細(xì)介紹了iOS中sqlite數(shù)據(jù)庫(kù)的原生用法,sqlite數(shù)據(jù)庫(kù)相信各位早已耳聞,非常輕巧的一個(gè)數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)僅一個(gè)文件,即建即用,感興趣的小伙伴們可以參考一下3
    2016-05-05
  • iOS實(shí)現(xiàn)一個(gè)意見反饋類型的輸入欄

    iOS實(shí)現(xiàn)一個(gè)意見反饋類型的輸入欄

    這篇文章主要給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)一個(gè)意見反饋類型的輸入欄,通過(guò)文中實(shí)現(xiàn)的輸入欄會(huì)用戶一個(gè)很好的體驗(yàn)效果,文中給了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • iOS開發(fā)教程之自定制圖片瀏覽器

    iOS開發(fā)教程之自定制圖片瀏覽器

    最近發(fā)現(xiàn)許多常用的APP都有圖片瀏覽器,于是想仿照著自己寫一個(gè),下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之自定制圖片瀏覽器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-12-12
  • 基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解

    基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解

    下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • iOS中UILabel text兩邊對(duì)齊的實(shí)現(xiàn)代碼

    iOS中UILabel text兩邊對(duì)齊的實(shí)現(xiàn)代碼

    本文通過(guò)一段實(shí)例代碼給大家介紹了ios中uilabel text兩邊對(duì)齊的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2017-01-01
  • ios中g(shù)etTime()的兼容性實(shí)例代碼

    ios中g(shù)etTime()的兼容性實(shí)例代碼

    在本篇文章里小編給大家整理的是關(guān)于ios中g(shù)etTime()的兼容性實(shí)例代碼,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • IOS如何在Host App 與 App Extension 之間發(fā)送通知

    IOS如何在Host App 與 App Extension 之間發(fā)送通知

    這篇文章主要介紹了IOS如何在Host App 與 App Extension 之間發(fā)送通知 的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • IOS CocoaPods詳細(xì)使用方法

    IOS CocoaPods詳細(xì)使用方法

    自從有了CocoaPods以后,這些繁雜的工作就不再需要我們親力親為了,只需要我們做好少量的配置工作,CocoaPods會(huì)為我們做好一切
    2016-09-09
  • IOS筆記061之二維碼的生成和掃描

    IOS筆記061之二維碼的生成和掃描

    隨著移動(dòng)設(shè)備的普及為二維碼提供了一個(gè)很好應(yīng)用平臺(tái),無(wú)論是在商城購(gòu)物還是美食城都離不開二維碼,本篇文章就給大家介紹IOS筆記061之二維碼的生成和掃描,感興趣的朋友可以過(guò)來(lái)一起學(xué)習(xí)啦,本文內(nèi)容講的很詳細(xì)
    2015-08-08

最新評(píng)論

双城市| 台山市| 阜宁县| 连州市| 文水县| 南充市| 邹平县| 行唐县| 咸宁市| 呼和浩特市| 台北县| 武汉市| 宁城县| 永吉县| 龙山县| 叙永县| 固原市| 曲沃县| 凤城市| 衢州市| 民和| 西丰县| 巨鹿县| 弥渡县| 揭西县| 县级市| 江安县| 清新县| 睢宁县| 师宗县| 德江县| 宜州市| 平江县| 泰宁县| 磐石市| 桃源县| 宁夏| 长兴县| 宝坻区| 兴文县| 当雄县|