当前位置: 移动技术网 > IT编程>移动开发>IOS > android中UIColletionView瀑布流布局实现思路以及封装的实现

android中UIColletionView瀑布流布局实现思路以及封装的实现

2019年07月24日  | 移动技术网IT编程  | 我要评论

邢台大峡谷,中世纪西秦帝国,Aum Lukkana

瀑布流实现思路

  • 第一种就是用scrollview来进行实现,由于它不具备复用的功能,因此我们需要自己写一套类似复用的模块来进行优化
  • 第二种就是利用apple做好的复用模块,自定义uicolletionlayout来实现瀑布流,想想也是第二种实现起来更快更优,ok,封装一个小小的框架来试试

默认两列

默认两列 

三列多倍间距

其他案例

  

上面的动画切换布局也是自定义uicollectionlayout来进行布局的,简单的静态图片布局展示其实就重写几个方法就可以了

1.preparelayout 每次重新刷新collectionview的时候会调用一次,做一些初始化的工作

2.layoutattributesforelementsinrect 返回已经制定好之后的每个cell对应的attribute属性对象进行布局

3.layoutattributesforitematindexpath 该方法会一直调用,每次cell出来就会根据对应的indexpath来进行方法调用,因此关键布局代码就可以放置在这里进行重新计算

4.collectionviewcontentsize 计算整体的大小,实现滚动

上面插入样式实现的传送门

瀑布流实现分析

1.基本变量的声明

// 每一列的间距
static const cgfloat mkjdefaultcolumnmargin = 10;
// 每一行间距
static const cgfloat mkjdefaultrowmargin = 10;
// 整体的上间距,左间距,下间距,右间距
static const uiedgeinsets mkjdefaultedgeinsets = {10,10,10,10};
// 默认是多少列
static const nsuinteger mkjdefaultcolumncounts = 2;

@interface mkjwaterfalllayout ()

@property (nonatomic,strong) nsmutablearray *attributearr; // cell属性的数组
@property (nonatomic,strong) nsmutablearray *columnheightarr; // 每列的高度数组

@end

2.初始化

// 每次刷新会调用一次
- (void)preparelayout
{
  [super preparelayout];

  // 每次重新刷新的时候清除之前的所有高度值,默认就是uiedg给定的top
  [self.columnheightarr removeallobjects];
  for (nsinteger i = 0; i < [self columncount]; i++) {
    [self.columnheightarr addobject:@([self insetmargin].top)];
  }
  // 每次刷新把对应的att属性清空
  [self.attributearr removeallobjects];
  // 初始化一次每个cell对应的attribute属性
  nsinteger count = [self.collectionview numberofitemsinsection:0];
  for (nsinteger i = 0; i < count; i++) {
    nsindexpath *indexpath = [nsindexpath indexpathforitem:i insection:0];
    uicollectionviewlayoutattributes *attribute = [self layoutattributesforitematindexpath:indexpath];
    [self.attributearr addobject:attribute];
  }
}

3.关键计算代码

// 返回attribute属性数组决定最后的排布
- (nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect
{
  return self.attributearr;
}

// 返回对应的indexpath下每个cell的属性 cell的出现会一直刷新该方法
- (uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath
{
  // 初始化布局属性---> 对应的indexpath
  uicollectionviewlayoutattributes *att = [uicollectionviewlayoutattributes layoutattributesforcellwithindexpath:indexpath];

  cgfloat collectionw = self.collectionview.frame.size.width;

  // 宽度是根据列数和间距固定算出来的
  cgfloat width = (collectionw - [self insetmargin].left - [self insetmargin].right - ([self columncount] - 1) * [self columnmargin]) / [self columncount];

  // 高度是根据代理的数据源返回比例计算的
  cgfloat height = [self.delegate mkjwaterfalllayout:self heightforitematindexpath:indexpath] * width;
  // x 和 y值都是在找出最小column之后才能确定,核心就是根据列数,找出最小高度的那一列
  // 先取出第一个默认是最小的
  cgfloat mincolumnheight = [self.columnheightarr[0] doublevalue];
  // 默认最小的是第0列
  nsuinteger finalcol = 0;
  for (nsinteger i = 1 ; i < [self columncount]; i++) {
    cgfloat currentcolheight = [self.columnheightarr[i] doublevalue];
    if (mincolumnheight > currentcolheight) {
      mincolumnheight = currentcolheight;
      finalcol = i;
    }
  }

  // x,y值是根据最小高度列算出来的
  cgfloat x = [self insetmargin].left + (width + [self columnmargin]) * finalcol;
  cgfloat y = mincolumnheight;
  // 当你是一个行排布的时候 默认是top值,不需要加间距
  nsinteger count = indexpath.item;
  if ((count / ([self columncount])) >= 1) {
    y += [self rowmargin];
  }
  att.frame = cgrectmake(x, y, width, height);
  self.columnheightarr[finalcol] = @(cgrectgetmaxy(att.frame));
  return att;

}

这里的计算简概括为就是对每个cell进行frame的计算

1.宽度的计算是根据列间距和整体左右间距以及行数进行限制,这些参数可以是固定值,也可以是代理传进去的

2.高度的计算必定是根据外部代理进行计算的

3.x值的计算是根据这个框架内部的每一列的高度数组进行之前的缓存高度,进行最小值计算,然后拿出最小值对应的列数,根据上面算出来的高度进行x值的计算

4.y值的计算和x值一样,根据给定的数组,比出最小高度列的列数,根据数组的高度,计算出对应的y值,最终再进行数组对应列数的高度更新

4.获取实际能容大小,让其可以滚动

// 计算出滚动区域的大小
- (cgsize)collectionviewcontentsize
{
  cgfloat maxcolumheight = [self.columnheightarr[0] doublevalue];
  for (nsinteger i = 1; i < [self columncount]; i++) {
    cgfloat currentcolheight = [self.columnheightarr[i] doublevalue];
    if (maxcolumheight < currentcolheight) {
      maxcolumheight = currentcolheight;
    }
  }
  return cgsizemake(0, maxcolumheight + [self insetmargin].bottom);

}

5.这个小框架已经封装好了,简单介绍下用法

// 声明如下
- (uicollectionview *)colletionview
{
  if (_colletionview == nil) {
    mkjwaterfalllayout *layout = [[mkjwaterfalllayout alloc] init];
    layout.delegate = self;
    uicollectionview *collectionv = [[uicollectionview alloc] initwithframe:[uiscreen mainscreen].bounds collectionviewlayout:layout];
    collectionv.backgroundcolor = [uicolor redcolor];
    [collectionv registernib:[uinib nibwithnibname:productid bundle:nil] forcellwithreuseidentifier:productid];
    collectionv.delegate = self;
    collectionv.datasource = self;
    _colletionview = collectionv;

  }
  return _colletionview;
}

// 内部行数,间距的控制
#pragma mark - waterfalllayoutdelegate
- (cgfloat)mkjwaterfalllayout:(mkjwaterfalllayout *)layout heightforitematindexpath:(nsindexpath *)indexpath
{
  // 返回宽度和高度比例
  mkjproductmodel *product = self.datasource[indexpath.item];
  return product.h / product.w;
}

// 控制列间距
- (cgfloat)columnmarginforwaterfalllayout:(mkjwaterfalllayout *)collectionviewlayout
{
  return 10;
}
// 控制行间距
- (cgfloat)rowmarginforwaterfalllayout:(mkjwaterfalllayout *)collectionviewlayout
{
  return 30;
}
// 控制列数
- (nsuinteger)columncountforwaterfalllayout:(mkjwaterfalllayout *)collectionviewlayout
{
//  if (self.datasource.count > 50) {
//    return 3;
//  }
  return 3;
}
// 控制整体上左下右间距
- (uiedgeinsets)insetforwaterfalllayout:(mkjwaterfalllayout *)collectionviewlayout
{
  return uiedgeinsetsmake(10, 10, 10, 10);
}

demo地址:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网