支持靠左,居中,靠右,等间距对齐。

靠左等间距.png

居中等间距.png

靠右等间距.png
  1. #import <UIKit/UIKit.h>
  2. typedef NS_ENUM(NSInteger,AlignType){
  3. AlignWithLeft,
  4. AlignWithCenter,
  5. AlignWithRight
  6. };
  7. @interface EqualSpaceFlowLayoutEvolve : UICollectionViewFlowLayout
  8. //两个Cell之间的距离
  9. @property (nonatomic,assign)CGFloat betweenOfCell;
  10. //cell对齐方式
  11. @property (nonatomic,assign)AlignType cellType;
  12. -(instancetype)initWthType : (AlignType)cellType;
  13. @end
  1. #import "EqualSpaceFlowLayoutEvolve.h"
  2. @interface EqualSpaceFlowLayoutEvolve(){
  3. //在居中对齐的时候需要知道这行所有cell的宽度总和
  4. CGFloat _sumWidth ;
  5. }
  6. @end
  7. @implementation EqualSpaceFlowLayoutEvolve
  8. -(instancetype)init{
  9. self = [super init];
  10. if (self){
  11. self.scrollDirection = UICollectionViewScrollDirectionVertical;
  12. self.minimumLineSpacing = 5;
  13. self.minimumInteritemSpacing = 5;
  14. self.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
  15. _betweenOfCell = 5.0;
  16. _cellType = AlignWithLeft;
  17. }
  18. return self;
  19. }
  20. -(instancetype)initWthType:(AlignType)cellType{
  21. self = [super init];
  22. if (self){
  23. self.scrollDirection = UICollectionViewScrollDirectionVertical;
  24. self.minimumLineSpacing = 5;
  25. self.minimumInteritemSpacing = 5;
  26. self.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
  27. _betweenOfCell = 5.0;
  28. _cellType = cellType;
  29. }
  30. return self;
  31. }
  32. -(void)setBetweenOfCell:(CGFloat)betweenOfCell{
  33. _betweenOfCell = betweenOfCell;
  34. self.minimumInteritemSpacing = betweenOfCell;
  35. }
  36. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
  37. NSArray * layoutAttributes_t = [super layoutAttributesForElementsInRect:rect];
  38. NSArray * layoutAttributes = [[NSArray alloc]initWithArray:layoutAttributes_t copyItems:YES];
  39. //用来临时存放一行的Cell数组
  40. NSMutableArray * layoutAttributesTemp = [[NSMutableArray alloc]init];
  41. for (NSUInteger index = 0; index < layoutAttributes.count ; index++) {
  42. UICollectionViewLayoutAttributes *currentAttr = layoutAttributes[index]; // 当前cell的位置信息
  43. UICollectionViewLayoutAttributes *previousAttr = index == 0 ? nil : layoutAttributes[index-1]; // 上一个cell 的位置信
  44. UICollectionViewLayoutAttributes *nextAttr = index + 1 == layoutAttributes.count ?
  45. nil : layoutAttributes[index+1];//下一个cell 位置信息
  46. //加入临时数组
  47. [layoutAttributesTemp addObject:currentAttr];
  48. _sumWidth += currentAttr.frame.size.width;
  49. CGFloat previousY = previousAttr == nil ? 0 : CGRectGetMaxY(previousAttr.frame);
  50. CGFloat currentY = CGRectGetMaxY(currentAttr.frame);
  51. CGFloat nextY = nextAttr == nil ? 0 : CGRectGetMaxY(nextAttr.frame);
  52. //如果当前cell是单独一行
  53. if (currentY != previousY && currentY != nextY){
  54. if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
  55. [layoutAttributesTemp removeAllObjects];
  56. _sumWidth = 0.0;
  57. }else if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionFooter]){
  58. [layoutAttributesTemp removeAllObjects];
  59. _sumWidth = 0.0;
  60. }else{
  61. [self setCellFrameWith:layoutAttributesTemp];
  62. }
  63. }
  64. //如果下一个不cell在本行,则开始调整Frame位置
  65. else if( currentY != nextY) {
  66. [self setCellFrameWith:layoutAttributesTemp];
  67. }
  68. }
  69. return layoutAttributes;
  70. }
  71. -(void)setCellFrameWith:(NSMutableArray*)layoutAttributes{
  72. CGFloat nowWidth = 0.0;
  73. switch (_cellType) {
  74. case AlignWithLeft:
  75. nowWidth = self.sectionInset.left;
  76. for (UICollectionViewLayoutAttributes * attributes in layoutAttributes) {
  77. CGRect nowFrame = attributes.frame;
  78. nowFrame.origin.x = nowWidth;
  79. attributes.frame = nowFrame;
  80. nowWidth += nowFrame.size.width + self.betweenOfCell;
  81. }
  82. _sumWidth = 0.0;
  83. [layoutAttributes removeAllObjects];
  84. break;
  85. case AlignWithCenter:
  86. nowWidth = (self.collectionView.frame.size.width - _sumWidth - (layoutAttributes.count * _betweenOfCell)) / 2;
  87. for (UICollectionViewLayoutAttributes * attributes in layoutAttributes) {
  88. CGRect nowFrame = attributes.frame;
  89. nowFrame.origin.x = nowWidth;
  90. attributes.frame = nowFrame;
  91. nowWidth += nowFrame.size.width + self.betweenOfCell;
  92. }
  93. _sumWidth = 0.0;
  94. [layoutAttributes removeAllObjects];
  95. break;
  96. case AlignWithRight:
  97. nowWidth = self.collectionView.frame.size.width - self.sectionInset.right;
  98. for (NSInteger index = layoutAttributes.count - 1 ; index >= 0 ; index-- ) {
  99. UICollectionViewLayoutAttributes * attributes = layoutAttributes[index];
  100. CGRect nowFrame = attributes.frame;
  101. nowFrame.origin.x = nowWidth - nowFrame.size.width;
  102. attributes.frame = nowFrame;
  103. nowWidth = nowWidth - nowFrame.size.width - _betweenOfCell;
  104. }
  105. _sumWidth = 0.0;
  106. [layoutAttributes removeAllObjects];
  107. break;
  108. }
  109. }
  110. @end

UICollectionView中Cell左对齐 居中 右对齐 等间距------你想要的,这里都有的更多相关文章

  1. markdown居中对齐,左对齐,右对齐

    Markdown语法本身没有居中,但Markdown中支持基本的HTMl语法,可以使用HTML语法. 居中: <center>居中</center> 左对齐: <p al ...

  2. 音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系

    音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系 原创 2014年02月11日 13:56:51 4951 0 0 刚刚过完春节,受假期综合症影响脑袋有点发 ...

  3. SQL中的左连接与右连接,内连接有什么不同

    SQL中的左连接与右连接,内连接有什么不同 我们来举个例子.天庭上面有一个管理系统:管理系统有个主表:主表记录着各个神仙的基本信息(我们把它当成表A).还有个表记录着他们这个神仙的详细信息(我们把它当 ...

  4. C++中的左值与右值(二)

    以前以为自己把左值和右值已经弄清楚了,果然发现自己还是太年轻了,下面的这些东西是自己通过在网上拾人牙慧,加上自己的理解写的. 1. 2. 怎么区分左值和右值:知乎大神@顾露的回答. 3. 我们不能直接 ...

  5. C++中的左值和右值

    左值和右值的定义 在C++中,能够放到赋值操作符=左边的是左值,能够放到赋值操作符右边的是右值.有些变量既能够当左值又能够当右值.进一步来讲,左值为Lvalue,事实上L代表Location,表示在内 ...

  6. c++中的左值与右值

    左值(lvalue)和右值(rvalue)是 c/c++ 中一个比较晦涩基础的概念,不少写了很久c/c++的人甚至没有听过这个名字,但这个概念到了 c++11 后却变得十分重要,它们是理解 move/ ...

  7. c++中的左值和右值的理解

    1.左值和右值的概念 C++中左值(lvalue)和右值(rvalue)是比较基础的概念,虽然平常几乎用不到,但C++11之后变得十分重要,它是理解 move/forward 等新语义的基础. 左值与 ...

  8. Android -如何在底部左对齐,中对齐,右对齐三个按钮图片 巧用Relative Layout

    Relative Layout 不仅可以指定同级的元素之间的位置关系(e.g. layout_toLeftOf) 还可以指定子元素与父元素之间的位置关系(e.g. layout_alignParent ...

  9. Android学习——LinearLayout布局实现居中、左对齐、右对齐

    android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...

随机推荐

  1. 阿里消息队列中间件 RocketMQ源码解析:Message发送&接收

  2. 16. leetcode 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example:     3    / \   9  20     /  \    15 ...

  3. 蓝桥杯算法训练_2的次幂表示+前缀表达式+Anagrams问题+出现次数最多的整数

    今天做了4个简单的题,题目虽然是简单,但是对于我这样的小白,还是有很多东西需要学习的. 2的次幂表示 上面就是题目,题目说的也很清晰了,接下来就是递归的实现: #include<iostream ...

  4. 设计模式-前摄器模式(Proactor)

    本周要进行boost asio库的学习,在学习之前发现最好需要先了解一下前摄器模式,这样对asio库的理解很有帮助,故写下此文 我之前写的随笔XShell的模拟实现中的链接方式可以说是同步的(服务器阻 ...

  5. linux C语言处理正则表达式

    Linux下C语言处理正则表达式——regex.h 具体函数介绍 编译正则表达式函数 int regcomp(regex_t *preg, const char *regex, int cflags) ...

  6. CSS基础学习笔记

    一. CSS介绍 1. CSS概述:CSS(Cascading Style Sheets)指层叠样式表,极大提高了工作效率. 2. 基础语法: 属性大于1个之后,属性之间用分号隔开 如果大于1个单词, ...

  7. 修改maven的默认JDK

    在我们实际使用IDEA开发maven项目的时候,创建maven项目的默认版本是jdk1.5,当然我们可以通过其他手段去修改module的JDK版本,或JDK的编译级别等等,但是如果每次你都这样修改,那 ...

  8. shell 编程之函数

    shell 函数的定义和普通的c语言函数定义差不多 function(){ } shell 函数的返回值,可以显示的return 语句,如果没有return  那么就会把最后一条语句的执行结果作为返回 ...

  9. 关于C#的学习心得体会

    1·多看多写 多看网上成熟的demo,养成一个良好的代码编写习惯,将终生受益 2·多编多敲 看了代码,理解demo中的思路,灵活运用到自己的代码中,这样不仅了解了别人的代码,同时还了解了代码的执行过程 ...

  10. 【Zookeeper】角色、顺序号、读写机制

    角色 leader 负责进行投票的发起和决议,更新系统状态 learner 包括follower和observer follower用于接受客户端请求并向客户端返回结果,在选举过程中参与投票 obse ...