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

靠左等间距.png

居中等间距.png

靠右等间距.png
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,AlignType){
AlignWithLeft,
AlignWithCenter,
AlignWithRight
}; @interface EqualSpaceFlowLayoutEvolve : UICollectionViewFlowLayout
//两个Cell之间的距离
@property (nonatomic,assign)CGFloat betweenOfCell;
//cell对齐方式
@property (nonatomic,assign)AlignType cellType; -(instancetype)initWthType : (AlignType)cellType; @end
#import "EqualSpaceFlowLayoutEvolve.h"

@interface EqualSpaceFlowLayoutEvolve(){
//在居中对齐的时候需要知道这行所有cell的宽度总和
CGFloat _sumWidth ;
}
@end @implementation EqualSpaceFlowLayoutEvolve -(instancetype)init{
self = [super init];
if (self){
self.scrollDirection = UICollectionViewScrollDirectionVertical;
self.minimumLineSpacing = 5;
self.minimumInteritemSpacing = 5;
self.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
_betweenOfCell = 5.0;
_cellType = AlignWithLeft;
}
return self;
} -(instancetype)initWthType:(AlignType)cellType{
self = [super init];
if (self){
self.scrollDirection = UICollectionViewScrollDirectionVertical;
self.minimumLineSpacing = 5;
self.minimumInteritemSpacing = 5;
self.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
_betweenOfCell = 5.0;
_cellType = cellType;
}
return self;
}
-(void)setBetweenOfCell:(CGFloat)betweenOfCell{
_betweenOfCell = betweenOfCell;
self.minimumInteritemSpacing = betweenOfCell;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray * layoutAttributes_t = [super layoutAttributesForElementsInRect:rect];
NSArray * layoutAttributes = [[NSArray alloc]initWithArray:layoutAttributes_t copyItems:YES];
//用来临时存放一行的Cell数组
NSMutableArray * layoutAttributesTemp = [[NSMutableArray alloc]init];
for (NSUInteger index = 0; index < layoutAttributes.count ; index++) { UICollectionViewLayoutAttributes *currentAttr = layoutAttributes[index]; // 当前cell的位置信息
UICollectionViewLayoutAttributes *previousAttr = index == 0 ? nil : layoutAttributes[index-1]; // 上一个cell 的位置信
UICollectionViewLayoutAttributes *nextAttr = index + 1 == layoutAttributes.count ?
nil : layoutAttributes[index+1];//下一个cell 位置信息 //加入临时数组
[layoutAttributesTemp addObject:currentAttr];
_sumWidth += currentAttr.frame.size.width; CGFloat previousY = previousAttr == nil ? 0 : CGRectGetMaxY(previousAttr.frame);
CGFloat currentY = CGRectGetMaxY(currentAttr.frame);
CGFloat nextY = nextAttr == nil ? 0 : CGRectGetMaxY(nextAttr.frame);
//如果当前cell是单独一行
if (currentY != previousY && currentY != nextY){
if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
[layoutAttributesTemp removeAllObjects];
_sumWidth = 0.0;
}else if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionFooter]){
[layoutAttributesTemp removeAllObjects];
_sumWidth = 0.0;
}else{
[self setCellFrameWith:layoutAttributesTemp];
}
}
//如果下一个不cell在本行,则开始调整Frame位置
else if( currentY != nextY) {
[self setCellFrameWith:layoutAttributesTemp];
}
}
return layoutAttributes;
} -(void)setCellFrameWith:(NSMutableArray*)layoutAttributes{
CGFloat nowWidth = 0.0;
switch (_cellType) {
case AlignWithLeft:
nowWidth = self.sectionInset.left;
for (UICollectionViewLayoutAttributes * attributes in layoutAttributes) {
CGRect nowFrame = attributes.frame;
nowFrame.origin.x = nowWidth;
attributes.frame = nowFrame;
nowWidth += nowFrame.size.width + self.betweenOfCell;
}
_sumWidth = 0.0;
[layoutAttributes removeAllObjects];
break;
case AlignWithCenter:
nowWidth = (self.collectionView.frame.size.width - _sumWidth - (layoutAttributes.count * _betweenOfCell)) / 2;
for (UICollectionViewLayoutAttributes * attributes in layoutAttributes) {
CGRect nowFrame = attributes.frame;
nowFrame.origin.x = nowWidth;
attributes.frame = nowFrame;
nowWidth += nowFrame.size.width + self.betweenOfCell;
}
_sumWidth = 0.0;
[layoutAttributes removeAllObjects];
break; case AlignWithRight:
nowWidth = self.collectionView.frame.size.width - self.sectionInset.right;
for (NSInteger index = layoutAttributes.count - 1 ; index >= 0 ; index-- ) {
UICollectionViewLayoutAttributes * attributes = layoutAttributes[index];
CGRect nowFrame = attributes.frame;
nowFrame.origin.x = nowWidth - nowFrame.size.width;
attributes.frame = nowFrame;
nowWidth = nowWidth - nowFrame.size.width - _betweenOfCell;
}
_sumWidth = 0.0;
[layoutAttributes removeAllObjects];
break; }
} @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. Redis-简单实现星形主从配置

    高级参考(https://www.zhihu.com/question/21419897) 简单应用场景 现在配置redis 星形 集群, 有三台服务器, 怎样实现? 复制redis.conf两份, ...

  2. 在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  3. 某安寿险HR核人面试问题总结,技术面波澜不惊,HR面反而有被暴击感

    之前朋友推荐了某安寿险的技术类职位.经过IQ/EQ网上笔试,技术面之前的java基础笔试和技术面试,基本上没什么意外的.昨天晚上7点安排的HR核人面试(他们的人力部门的工作负荷比较大,目前这个部门处于 ...

  4. 什么是IAT重定向

    例壳:telock 0.98 仅允许非商业转载,转载请注明出处

  5. Andrew Ng机器学习课程笔记(五)之应用机器学习的建议

    Andrew Ng机器学习课程笔记(五)之 应用机器学习的建议 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7368472.h ...

  6. Linux操作系统-命令-netstat

    # 之前已经写过了3篇与"性能测试"有关系的Linux命令,它们分别是free.top.vmstat # 接下来还需要把另外2个命令也写下来:netstat和iostat 最近认真地读了1篇关于"定位 ...

  7. HTML5的三种存储方式以及区别

    首先将存储方式前要先知道为什么要使用他:一是利用本地数据,介绍网络请求:二是弱网环境下,高延迟,低带宽,要把数据本地化: 1.本地存储localStorage和sessionStorage 介绍: 存 ...

  8. 200行的Node爬虫花了半天的时间把网易云上的30万首歌曲信息都抓取回来了

    早两天在网易云听歌看评论的时候,突然想把网易云上所有歌曲都抓取下来然后按照评论数进行一次排名,把评论数超过10万的歌曲都听一次,于是便有了这个项目. 因为只是一个小前端,所以使用了Node来写这个爬虫 ...

  9. C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能

    1.IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环 ...

  10. 如何正确地修改.data和.item文件的‘utf-8’格式

    问题:有时候,我们在做分类信息提取的时候文件的格式可能不是我们想要的.txt文件的格式,如何进行修改? 解决:(1)将文件复制一份,并保存为.txt的格式(2)将复制之后的文件通过另存为的方式更改为u ...