当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS封装自定义布局的方法

IOS封装自定义布局的方法

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

云雨禁恋,李连杰 国籍,黑天鹅颜月溪

一、概述
1、对于经常使用的控件或类,通常将其分装为一个单独的类来供外界使用,以此达到事半功倍的效果
2、由于分装的类不依赖于其他的类,所以若要使用该类,可直接将该类拖进项目文件即可
3、在进行分装的时候,通常需要用到代理设计模式
二、代理设计模式
1、代理设计模式的组成
客户类(通常作为代理):通常委托这是角色来完成业务逻辑
真实角色:将客户类的业务逻辑转化为方法列表,即代理协议
代理协议:

  • 定义了需要实现的业务逻辑
  • 定义了一组方法列表,包括必须实现的方法或选择实现的方法
  • 代理协议是代理对象所要遵循一组规则

代理角色

  • 若要作为代理,需要遵守代理协议,并且实现必须实现的代理方法
  • 代理角色可以通过调用代理协议中的方法完成业务逻辑,也可以附加自己的操作

文字描述通常是抽象的,一下通过图示来阐述代理设计模式

三、自定义布局类的封装
1、业务逻辑
如图

2、布局每个cell的业务逻辑
由于设置每个cell的布局属性的业务逻辑较复杂,特附上如下思维导图

3、封装思路封装需要根据客户类业务逻辑需求来提供接口
1)、通过代理协议的可选实现的方法获取的属性值的属性,需要设置默认值
2)、未提供默认值的且必须使用的属性,需要通过必须实现的方法来获得
3)、自定义布局提供的接口可选

  • 列数
  • 列之间的间距
  • 行之间的间距
  • 内边距

4)、自定义布局提供的接口必选
每个元素的高度,宽度可以通过列数和列间距计算得到
四、封装步骤
设置代理协议,提供接口

//声明lypwaterflowlayout为一个类
@class lypwaterflowlayout;
@protocol lypwaterflowlayoutdelegate <nsobject>
//必须实现的方法
@required
/**获取瀑布流每个元素的高度*/
- (cgfloat)waterflowlayout:(lypwaterflowlayout *)waterflowlayout heightforitematindex:(nsinteger)index itemwith:(cgfloat)itemwith;
//可选实现的方法
@optional
/**获取瀑布流的列数*/
- (nsinteger)columncountinwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
/**获取瀑布流列间距*/
- (cgfloat)columnmargininwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
/**获取瀑布流的行间距*/
- (cgfloat)rowmargininwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
/**获取瀑布流的内边距*/
- (uiedgeinsets)edgeinsetsinwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
@end

设置代理属性

@interface lypwaterflowlayout : uicollectionviewlayout
/**代理*/
@property (nonatomic, weak) id<lypwaterflowlayoutdelegate> delegate;
@end

设置通过可选代理方法获取属性值的属性的默认值

/**默认的列数*/
static const nsinteger lypdefaultcolumncount = 3;
/**默认每一列之间的间距*/
static const cgfloat lypdefaultcolummargin = 10;
/**默认每一行之间的间距*/
static const cgfloat lypdefaultrowmargin = 10;
/**默认边缘间距*/
static const uiedgeinsets lypdefaultedgeinsets = {10, 10, 10, 10};

设置通过可选代理方法获取属性值的属性的访问方式若代理提供属性值,则忽略默认值

- (nsinteger)columncount
{
  //判断代理是否实现了获取列数的可选方法
  if ([self.delegate respondstoselector:@selector(columncountinwaterflowlayout:)])
  {
    //实现,返回通过代理设置的列数
    return [self.delegate columncountinwaterflowlayout:self];
  }
  else
  {
    //为实现,返回默认的列数
    return lypdefaultcolumncount;
  }
}

注:其他属性值的获取与上述方法几乎完全相同,不再赘述
设置布局
1)、设置需要的成员属性

/**所有cell的布局属性*/
@property (nonatomic, strong) nsmutablearray *attrsarray;
/**所有列的当前高度*/
@property (nonatomic, strong) nsmutablearray *columnheights;

2)、通过懒加载的方式初始化成员属性

/**--attrsarray--懒加载*/
- (nsmutablearray *)attrsarray
{
  if (_attrsarray == nil)
  {
    _attrsarray = [nsmutablearray array];
  }
  return _attrsarray;
}
/**--columnheights--懒加载*/
- (nsmutablearray *)columnheights
{
  if (_columnheights == nil)
  {
    _columnheights = [nsmutablearray array];
  }
  return _columnheights;
}

3)、初始化布局

- (void)preparelayout
{
  [super preparelayout];

  /**清除之前跟布局相关的所有属性,重新设置新的布局*/
  //清除之前计算的所有列的高度
  [self.columnheights removeallobjects];
  //设置所有列的初始高度
  for (nsinteger i = 0; i<self.columncount; i++)
  {
    self.columnheights[i] = @(self.edgeinsets.top);
  }
  //清除之前所有的布局属性
  [self.attrsarray removeallobjects];

  /**开始创建每一个cell对应的布局属性*/
  nsinteger count = [self.collectionview numberofitemsinsection:0];
  for (nsinteger i = 0; i<count; i++)
  {
    nsindexpath *indexpath = [nsindexpath indexpathforitem:i insection:0];
    //获取indexpath位置cell对应的布局属性
    uicollectionviewlayoutattributes *attrs = [self layoutattributesforitematindexpath:indexpath];
    //将indexpath位置的cell的布局属性添加到所有cell的布局属性数组中
    [self.attrsarray addobject:attrs];
  }
}

4)、返回包含所有cell的布局属性的数组

- (nullable nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect
{
  return self.attrsarray;
}
设置每一个cell的布局属性

- (nullable uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nonnull nsindexpath *)indexpath
{
  //获取indexpath位置的布局属性
  uicollectionviewlayoutattributes *attrs = [uicollectionviewlayoutattributes layoutattributesforcellwithindexpath:indexpath];

  /**设置cell布局属性的frame*/

  /***确定cell的尺寸***/
  //获取collectionview的宽度
  cgfloat collectionviewwidth = self.collectionview.frame.size.width;
  //cell宽度
  cgfloat width = ((collectionviewwidth - self.edgeinsets.left - self.edgeinsets.right - (self.columncount - 1) * self.colummargin)) / self.columncount;
  //cell高度
  cgfloat height = [self.delegate waterflowlayout:self heightforitematindex:indexpath.item itemwith:width];

  /***设置cell的位置***/
  nsinteger destcolumn = 0;
  cgfloat mincolumnheight = [self.columnheights[0] doublevalue];
  for (nsinteger i = 1; i<self.columncount; i++)
  {
    cgfloat columnheight = [self.columnheights[i] doublevalue];
    if (mincolumnheight > columnheight)
    {
      mincolumnheight = columnheight;
      destcolumn = i;
    }
  }
  //计算cell的位置
  cgfloat x = self.edgeinsets.left + destcolumn * (width + self.colummargin);
  cgfloat y = mincolumnheight;
  //判断是不是第一行
  if (y != self.edgeinsets.top)
  {
    //若不是第一行,需要加上行间距
    y += self.rowmargin;
  }

  /**给cell的布局属性的frame赋值*/
  attrs.frame = cgrectmake(x, y, width, height);

  //更新最短那列的高度
  self.columnheights[destcolumn] = @(cgrectgetmaxy(attrs.frame));

  /**返回indexpath位置的cell的布局属性*/
  return attrs;
}

5)、设置collectionview内容的尺寸

- (cgsize)collectionviewcontentsize
{
  //获取最高的那一列的高度
  cgfloat maxcolumnheight = [self.columnheights[0] doublevalue];
  for (nsinteger i = 1; i<self.columncount; i++)
  {
    cgfloat columnheight = [self.columnheights[i] doublevalue];
    if (maxcolumnheight < columnheight)
    {
      maxcolumnheight = columnheight;
    }
  }
  //返回collectionview的contentsize,高度为最高的高度加上一个行间距
  return cgsizemake(0, maxcolumnheight + self.rowmargin);
}

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

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

相关文章:

验证码:
移动技术网