ios基于UICollectionView实现横向瀑布流

在网上找了许久,一直没有发现有提供横向瀑布流效果的。在项目中用到了我就在垂直瀑布流的基础上,进行了修改,做出了横向瀑布流的效果。同时也对一些UICollectionView的属性进行简单的注释,方便以后查阅。

1、首先要写一个继承与NSObject的布局类,记录每一行(列)目前的宽度(高度)。再添加一个新的cell的时候进行判断比较,添加到最短的那一行或一列上。

2、横向的布局类入下,垂直的话就是讲对应的X Y轴数据进行调整即可。
WaterfallFlowLayout为布局类,继承与NSObject。.h文件入下

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

@protocol WaterfallFlowLayoutDelegate <NSObject>
// 动态获取 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: 准备布局
-(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++) {
    // 初始化每一列的宽度(默认为上边距)
//    _heightOfColumns[i] = @(self.sectionInset.top);
    [_widthOfColumns addObject:@(self.sectionInset.left)];
  }
  // item的总数
  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;
  // 每一个item的高度
  CGFloat itemHeight = validHeight/self.numberOfColumns;

  // 设置item的默认宽度
  CGFloat itemWidth = itemHeight;
  for (NSInteger i = 0; i<count; i++) {
    // 最短列的下标
    NSInteger index = [self indexOfShortestColumn];
    CGFloat originY = self.sectionInset.top + index * (itemHeight +self.minimumInteritemSpacing);
    CGFloat originX = [_widthOfColumns[index] floatValue];
    // 构造 indexPath
    NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0];
    // 动态的获取宽度
    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 的属性 到数组中
    [_itemsAttributes addObject:attr];
    // 更新布局到的一列(最短列) 的高度
    _widthOfColumns[index] = @(originX + itemWidth + self.minimumLineSpacing);
  }
  // 刷新显示
  [self.collectionView reloadData];
}

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

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

//重写方法 3: 返回内容的尺寸
-(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、上边的这个布局类可以直接复制粘贴下来。然后就是创建你的UICollectionView

在collectionView的cell中可以直接创建imageView或者是label添加到cell上,用来显示数据。
collectionView默认section缩进左右是0
调节横向cell间距
layout.minimumLineSpacing = 10;
调节纵向cell间距
layout.minimumInteritemSpacing = 20;
调节瀑布流显示的行数,当然了你的collectionView的高(宽)足够显示几行(列)就会自动显示多上行(列);
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];
  // 创建集合视图
  [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];
  // 设置代理
  _collectionView.delegate = self;
  _collectionView.dataSource = self;

  // 注册cell 类型 及 复用标识
  [_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(@"点击了第 %ld 组,第 %ld 行",indexPath.section,indexPath.row);
}

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

@end

实现的效果如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • .NetCore利用BlockingCollection实现简易消息队列

    消息队列现今的应用场景越来越大,常用的有RabbmitMQ和KafKa. 我们用BlockingCollection来实现简单的消息队列. BlockingCollection实现了生产者/消费者模式,是对IProducerConsumerCollection<T>接口的实现.与其他Concurrent集合一样,每次Add或Take元素,都会导致对集合的lock.只有当确定需要在内存中创建一个生产者,消费者模式时,再考虑这个类. MSDN中的示例用法: using (BlockingColle

  • JAVA集合框架工具类自定义Collections集合方法

    项目中有需要多次统计 某些集合中 的某个属性值,所以考虑封装一个方法,让其其定义实现计算方式. 话不多说,看代码: 1.封装的自定义集合工具类:CollectionsCustom package com.test.util; import java.util.Collection; import org.apache.commons.collections.CollectionUtils; /** * 自定义集合处理类 */ public class CollectionsCustom { /*

  • Python中collections模块的基本使用教程

    前言 之前认识了python基本的数据类型和数据结构,现在认识一个高级的:Collections,一个模块主要用来干嘛,有哪些类可以使用,看__init__.py就知道 '''This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict, list, set, and tuple. * named

  • Mybatis中collection和association的使用区别详解

    最近一直把collection和association弄混,所以为了增强自己的记忆,就撸一个关系出来算是总结罢了 1. 关联-association 2. 集合-collection 比如同时有User.java和Card.java两个类 User.java如下: public class User{ private Card card_one; private List<Card> card_many; } 在映射card_one属性时用association标签, 映射card_many时

  • MongoDB固定集合(capped collection)的知识小结

    一 . 什么是固定集合 MongoDB中有一种特殊类型的集合,值得我们特别留意,那就是固定集合(capped collection). 固定集合可以声明collection的容量大小,其行为类似于循环队列.数据插入时,新文档会被插入到队列的末尾,如果队列已经被占满,那么最老的文档会被之后插入的文档覆盖. 固定集合特性:固定集合很像环形队列,如果空间不足,最早的文档就会被删除,为新的文档腾出空间.一般来说,固定集合适用于任何想要自动淘汰过期属性的场景. 固定集合应用场景 比如日志文件,聊天记录,通

  • collection集合体系与并发修改异常的解决方法

    collection是单列集合的顶层接口,下面还包括了两个常用子接口  List.set List: list接口有两个实现的子类:特点是:有序且可重复 ArrayList的数据结构是数组结构 LinkedList的数据结构是链表结构 1.ArrayList:特点:查询快 增删慢  初始容量大小为10 扩充容量算法为    ((旧容量 * 3) / 2) + 1 如果你知道你的arrayList 会达到多少容量,可以在初始化的时候就指定,能节省扩容的性能开支 2.LinkedList:特点: 

  • iOS Swift利用UICollectionView实现无限轮播功能(原理)详解

    前言 作为一个资深(自认为)iOS程序猿,会经常用到轮播图,上一次使用UIScrollView实现无限轮播的效果,这一次在Swift语言中,我使用UICollectionView再为大家讲解一次无限轮播的实现原理. 先上图: UICollectionView-无限轮播.gif 首先需要实现了就是UICollectionView的分页,这个很简单: collectionView.isPagingEnabled = true 接下来就是原理,在UICollectionView的两端需要先添加两张图片

  • Java中Collection、List、Set、Map之间的关系总结

    初学java,单个的接触有点迷糊,所以总结下他们的关系 一.关系 Collection --List:以特定顺序存储 --ArrayList.LinkList.Vector --Set:不能包含重复的元素 --HashSet.TreeSet Map --HashMap.HashTable.TreeMap 二.分别讲解 Collection:Collection是一个父接口,List和Set是继承自他的子接口,Collection是最基本的集合接口,Java SDK中不提供直接继承自Collect

  • iOS开发UICollectionView实现拖拽效果

    一.介绍 iOS9提供API实现单元格排序功能,使用UICollectionView及其代理方法.iOS9之后有自带方法可以实现该效果,只需添加长按手势,实现手势方法和调用iOS9的API交换数据,iOS9之前需要自己写方法实现这效果,除了要添加长按手势,这里还需要利用截图替换原理,手动计算移动位置来处理视图交换和数据交换. 二.方法和步骤 1.创建工程项目和视图控制器,如下图 2.声明对象和设置代理和数据源代理 @interface ViewController ()<UICollection

  • python的常用模块之collections模块详解

    认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别:   1 使用python编写的代码(.py文件)   2 已被编译为共享库或DLL的C或C++扩展   3 包好一组模块的包   4 使用C编写并链接到python解释器的内置模块 为何要使用模块? 如果你退出python解释器然后重新进入,那么你之前定义的函数或者变量都将丢失,因此我们通常将程序写到文件中以便永久保存下来,

随机推荐