亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ios基于UICollectionView實(shí)現(xiàn)橫向瀑布流

 更新時(shí)間:2018年12月11日 08:39:35   作者:shengdaVolleyball  
這篇文章主要為大家詳細(xì)介紹了ios基于UICollectionView實(shí)現(xiàn)橫向瀑布流,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在網(wǎng)上找了許久,一直沒(méi)有發(fā)現(xiàn)有提供橫向瀑布流效果的。在項(xiàng)目中用到了我就在垂直瀑布流的基礎(chǔ)上,進(jìn)行了修改,做出了橫向瀑布流的效果。同時(shí)也對(duì)一些UICollectionView的屬性進(jìn)行簡(jiǎn)單的注釋,方便以后查閱。

1、首先要寫一個(gè)繼承與NSObject的布局類,記錄每一行(列)目前的寬度(高度)。再添加一個(gè)新的cell的時(shí)候進(jìn)行判斷比較,添加到最短的那一行或一列上。

2、橫向的布局類入下,垂直的話就是講對(duì)應(yīng)的X Y軸數(shù)據(jù)進(jìn)行調(diào)整即可。
WaterfallFlowLayout為布局類,繼承與NSObject。.h文件入下

#import <UIKit/UIKit.h>
// 類的前置聲明
@class WaterfallFlowLayout;

@protocol WaterfallFlowLayoutDelegate <NSObject>
// 動(dòng)態(tài)獲取 item 寬度
- (CGFloat) WaterfallFlowLayout:(WaterfallFlowLayout *) layout widthForItemAtIndexPath:(NSIndexPath *) indexPath;

@end

@interface WaterfallFlowLayout : UICollectionViewLayout

@property (nonatomic,assign) id <WaterfallFlowLayoutDelegate> delegate;

@property (nonatomic) NSInteger numberOfColumns;
@property (nonatomic) CGFloat minimumLineSpacing;
@property (nonatomic) CGFloat minimumInteritemSpacing;
@property (nonatomic) UIEdgeInsets sectionInset;
@end

WaterfallFlowLayout為布局類,繼承與NSObject。.m文件入下

#import "WaterfallFlowLayout.h"

@interface WaterfallFlowLayout ()
{
  // 用于記錄每一列布局到的寬度
  NSMutableArray * _widthOfColumns;
  // 用于保存所有item的屬性 (frame)
  NSMutableArray * _itemsAttributes;
}

@end


@implementation WaterfallFlowLayout
- (void) setNumberOfColumns:(NSInteger)numberOfColumns {
  if (_numberOfColumns != numberOfColumns) {
    _numberOfColumns = numberOfColumns;
    // 讓原有布局失效,需要重新布局
    [self invalidateLayout];
  }
}

- (void)setMinimumLineSpacing:(CGFloat)minimumLineSpacing {
  if (_minimumLineSpacing != minimumLineSpacing) {
    _minimumLineSpacing = minimumLineSpacing;
    [self invalidateLayout];
  }
}
- (void)setMinimumInteritemSpacing:(CGFloat)minimumInteritemSpacing {
  if (_minimumInteritemSpacing != minimumInteritemSpacing) {
    _minimumInteritemSpacing = minimumInteritemSpacing;
    [self invalidateLayout];
  }
}

- (void)setSectionInset:(UIEdgeInsets)sectionInset {
  if (!UIEdgeInsetsEqualToEdgeInsets(_sectionInset, sectionInset)) {
    _sectionInset = sectionInset;
    [self invalidateLayout];
  }
}

//重寫方法 1: 準(zhǔn)備布局
-(void)prepareLayout {
  [super prepareLayout];
  // 真正的布局在這里完成
  if (_itemsAttributes) {
    [_itemsAttributes removeAllObjects];
  }else {
    _itemsAttributes = [[NSMutableArray alloc] init];
  }
  if (_widthOfColumns) {
    [_widthOfColumns removeAllObjects];
  }else {
    _widthOfColumns = [[NSMutableArray alloc] init];
  }
  for (NSInteger i = 0; i < self.numberOfColumns; i++) {
    // 初始化每一列的寬度(默認(rèn)為上邊距)
//    _heightOfColumns[i] = @(self.sectionInset.top);
    [_widthOfColumns addObject:@(self.sectionInset.left)];
  }
  // item的總數(shù)
  NSInteger count = [self.collectionView numberOfItemsInSection:0];

//  CGFloat itemWidth = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (_numberOfColumns-1) * _minimumInteritemSpacing )/_numberOfColumns;

  // 總的高度 (集合視圖的寬度)
  CGFloat totalHeight = self.collectionView.frame.size.height;
  // 有效的高度 (出去間隔及邊界)
  CGFloat validHeight = totalHeight - self.sectionInset.top - self.self.sectionInset.bottom - (self.numberOfColumns-1) * self.minimumInteritemSpacing;
  // 每一個(gè)item的高度
  CGFloat itemHeight = validHeight/self.numberOfColumns;


  // 設(shè)置item的默認(rèn)寬度
  CGFloat itemWidth = itemHeight;
  for (NSInteger i = 0; i<count; i++) {
    // 最短列的下標(biāo)
    NSInteger index = [self indexOfShortestColumn];
    CGFloat originY = self.sectionInset.top + index * (itemHeight +self.minimumInteritemSpacing);
    CGFloat originX = [_widthOfColumns[index] floatValue];
    // 構(gòu)造 indexPath
    NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
    // 動(dòng)態(tài)的獲取寬度
    if ([self.delegate respondsToSelector:@selector(WaterfallFlowLayout:widthForItemAtIndexPath:)]) {
      itemWidth = [self.delegate WaterfallFlowLayout:self widthForItemAtIndexPath:indexPath];
    }
    UICollectionViewLayoutAttributes * attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

    attr.frame = CGRectMake(originX, originY, itemWidth, itemHeight);
    // 保存 item 的屬性 到數(shù)組中
    [_itemsAttributes addObject:attr];
    // 更新布局到的一列(最短列) 的高度
    _widthOfColumns[index] = @(originX + itemWidth + self.minimumLineSpacing);
  }
  // 刷新顯示
  [self.collectionView reloadData];
}


//重寫方法 2: 返回指定區(qū)域的item的屬性(frame)
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  NSMutableArray * array = [NSMutableArray array];

  for (UICollectionViewLayoutAttributes * attr in _itemsAttributes) {
    // 判斷兩個(gè)矩形是否有交集
    if (CGRectIntersectsRect(attr.frame, rect)) {
      [array addObject:attr];
    }
  }
  return array;
}


//重寫方法 3: 返回內(nèi)容的尺寸
-(CGSize)collectionViewContentSize {
  CGFloat height = self.collectionView.frame.size.height;
  NSInteger index = [self indexOfLongestColumn];
  CGFloat width = [_widthOfColumns[index] floatValue] + self.sectionInset.right - self.minimumLineSpacing;
  return CGSizeMake(width, height);
}


- (NSInteger) indexOfLongestColumn {
  NSInteger index = 0;
  for (NSInteger i = 0; i<_numberOfColumns; i++) {
    if ([_widthOfColumns[i] floatValue] > [_widthOfColumns[index] floatValue]) {
      index = i;
    }
  }

  return index;
}

- (NSInteger) indexOfShortestColumn {
  NSInteger index = 0;
  for (NSInteger i = 0; i<_numberOfColumns; i++) {
    if ([_widthOfColumns[i] floatValue] < [_widthOfColumns[index] floatValue]) {
      index = i;
    }
  }

  return index;
}
@end

3、上邊的這個(gè)布局類可以直接復(fù)制粘貼下來(lái)。然后就是創(chuàng)建你的UICollectionView

在collectionView的cell中可以直接創(chuàng)建imageView或者是label添加到cell上,用來(lái)顯示數(shù)據(jù)。
collectionView默認(rèn)section縮進(jìn)左右是0
調(diào)節(jié)橫向cell間距
layout.minimumLineSpacing = 10;
調(diào)節(jié)縱向cell間距
layout.minimumInteritemSpacing = 20;
調(diào)節(jié)瀑布流顯示的行數(shù),當(dāng)然了你的collectionView的高(寬)足夠顯示幾行(列)就會(huì)自動(dòng)顯示多上行(列);
layout.numberOfColumns = 3;

#import "RootViewController.h"
#import "WaterfallFlowLayout.h"

@interface RootViewController () <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,WaterfallFlowLayoutDelegate>
{
  UICollectionView * _collectionView;
}
@end

@implementation RootViewController
- (void)dealloc {
  [_collectionView release];
  [super dealloc];

}

- (void)viewDidLoad {
  [super viewDidLoad];
  // 創(chuàng)建集合視圖
  [self createCollectionView];
}

- (UICollectionViewLayout *)createLayout {
#if 1
  WaterfallFlowLayout * layout = [[WaterfallFlowLayout alloc] init];
  layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
  layout.minimumLineSpacing = 10;
  layout.minimumInteritemSpacing = 20;
  layout.numberOfColumns = 3;
  layout.delegate = self;
  [self performSelector:@selector(changeLayout:) withObject:layout afterDelay:3];

#else
  UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];

  layout.minimumLineSpacing = 10;
  layout.itemSize = CGSizeMake(150, 100);
  layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

#endif


  return [layout autorelease];
}
- (void)changeLayout:(WaterfallFlowLayout *)layout {
  layout.numberOfColumns = 3;
}


- (void)createCollectionView {
  CGRect frame = CGRectMake(0, 20, VIEW_WIDTH, VIEW_HEIGHT-20);
  _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:[self createLayout]];

  _collectionView.backgroundColor = [UIColor cyanColor];
  // 設(shè)置代理
  _collectionView.delegate = self;
  _collectionView.dataSource = self;

  // 注冊(cè)cell 類型 及 復(fù)用標(biāo)識(shí)
  [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];


  [self.view addSubview:_collectionView];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  return 102;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];

  UILabel * label = nil;
  NSArray * array = cell.contentView.subviews;
  if (array.count) {
    label = array[0];
  }else {
    label = [[UILabel alloc] init];
//    label.frame = cell.bounds;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:50];
    [cell.contentView addSubview:label];
    [label release];
  }
  label.frame = cell.bounds;
  label.text = [NSString stringWithFormat:@"%ld",indexPath.item];
  label.textColor = [UIColor whiteColor];
  cell.backgroundColor = RandomColor;

  return cell;
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  return CGSizeMake( arc4random()%100+200, 110);
}

-(CGFloat) WaterfallFlowLayout:(WaterfallFlowLayout *)layout widthForItemAtIndexPath:(NSIndexPath *)indexPath{
  return arc4random()%150+50;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  NSLog(@"點(diǎn)擊了第 %ld 組,第 %ld 行",indexPath.section,indexPath.row);
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}


@end

實(shí)現(xiàn)的效果如下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS、Mac OS X系統(tǒng)中編程實(shí)現(xiàn)漢字轉(zhuǎn)拼音的方法(超級(jí)簡(jiǎn)單)

    iOS、Mac OS X系統(tǒng)中編程實(shí)現(xiàn)漢字轉(zhuǎn)拼音的方法(超級(jí)簡(jiǎn)單)

    這篇文章主要介紹了iOS、Mac OS X系統(tǒng)中編程實(shí)現(xiàn)漢字轉(zhuǎn)拼音的方法(超級(jí)簡(jiǎn)單),本文講解的方法不僅支持中文,還支持日文、韓文等,需要的朋友可以參考下
    2015-04-04
  • 輕松搞定iOS本地消息推送

    輕松搞定iOS本地消息推送

    這篇文章主要幫助大家輕松搞定iOS本地消息推送,iOS中使用本地通知為你的APP添加提示用戶功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Flutter?RendererBinding作用源碼分析

    Flutter?RendererBinding作用源碼分析

    這篇文章主要為大家介紹了Flutter?RendererBinding作用和內(nèi)部一些重要的類源碼分析,希望此文能給你帶來(lái)收獲.有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法

    iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法

    本篇文章主要介紹了iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫示例代碼

    iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫示例代碼

    這篇文章主要給大家介紹了關(guān)于iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • iOS從系統(tǒng)相冊(cè)選取多張照片示例代碼

    iOS從系統(tǒng)相冊(cè)選取多張照片示例代碼

    本篇文章主要介紹了iOS從系統(tǒng)相冊(cè)選取多張照片示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • 超全的iOS各種設(shè)備信息獲取方法總結(jié)(包括iPhone8/iPhone X)

    超全的iOS各種設(shè)備信息獲取方法總結(jié)(包括iPhone8/iPhone X)

    這篇文章主要給大家介紹了關(guān)于iOS各種設(shè)備信息獲取方法,iPhone8/iPhone X的后驅(qū)詳細(xì)信息也已更新,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Flutter列表滾動(dòng)定位超強(qiáng)輔助庫(kù)使用示例詳解

    Flutter列表滾動(dòng)定位超強(qiáng)輔助庫(kù)使用示例詳解

    這篇文章主要為大家介紹了Flutter列表滾動(dòng)定位超強(qiáng)輔助庫(kù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 解決Xcode8打包上傳構(gòu)建版本無(wú)效的辦法

    解決Xcode8打包上傳構(gòu)建版本無(wú)效的辦法

    這篇文章主要介紹的是自己在打包上傳項(xiàng)目的時(shí)候遇到的一個(gè)問(wèn)題,通過(guò)自己的努力一步步解決了,現(xiàn)將解決方法方法分享給大家,希望給同樣遇到這個(gè)問(wèn)題的朋友們能有所幫助,下面來(lái)一起看看吧。
    2016-09-09
  • iOS10實(shí)現(xiàn)推送功能時(shí)的注意點(diǎn)和問(wèn)題總結(jié)

    iOS10實(shí)現(xiàn)推送功能時(shí)的注意點(diǎn)和問(wèn)題總結(jié)

    很多朋友都反饋,發(fā)現(xiàn)了iOS9升級(jí)到iOS10推送功能不正常的問(wèn)題,所以這篇文章總結(jié)了一下要點(diǎn),親們可以根據(jù)以下步驟,逐步排查問(wèn)題,也可以逐步實(shí)現(xiàn)iOS10的推送功能。下面來(lái)一起看看吧。
    2016-09-09

最新評(píng)論