UICollectionView中Cell左对齐 居中 右对齐 等间距------你想要的,这里都有
支持靠左,居中,靠右,等间距对齐。
![](http://upload-images.jianshu.io/upload_images/1623179-f974348a80c6b191.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/1623179-d6345c55436463f1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/1623179-af2eb1beb6502155.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#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左对齐 居中 右对齐 等间距------你想要的,这里都有的更多相关文章
- markdown居中对齐,左对齐,右对齐
Markdown语法本身没有居中,但Markdown中支持基本的HTMl语法,可以使用HTML语法. 居中: <center>居中</center> 左对齐: <p al ...
- 音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系
音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系 原创 2014年02月11日 13:56:51 4951 0 0 刚刚过完春节,受假期综合症影响脑袋有点发 ...
- SQL中的左连接与右连接,内连接有什么不同
SQL中的左连接与右连接,内连接有什么不同 我们来举个例子.天庭上面有一个管理系统:管理系统有个主表:主表记录着各个神仙的基本信息(我们把它当成表A).还有个表记录着他们这个神仙的详细信息(我们把它当 ...
- C++中的左值与右值(二)
以前以为自己把左值和右值已经弄清楚了,果然发现自己还是太年轻了,下面的这些东西是自己通过在网上拾人牙慧,加上自己的理解写的. 1. 2. 怎么区分左值和右值:知乎大神@顾露的回答. 3. 我们不能直接 ...
- C++中的左值和右值
左值和右值的定义 在C++中,能够放到赋值操作符=左边的是左值,能够放到赋值操作符右边的是右值.有些变量既能够当左值又能够当右值.进一步来讲,左值为Lvalue,事实上L代表Location,表示在内 ...
- c++中的左值与右值
左值(lvalue)和右值(rvalue)是 c/c++ 中一个比较晦涩基础的概念,不少写了很久c/c++的人甚至没有听过这个名字,但这个概念到了 c++11 后却变得十分重要,它们是理解 move/ ...
- c++中的左值和右值的理解
1.左值和右值的概念 C++中左值(lvalue)和右值(rvalue)是比较基础的概念,虽然平常几乎用不到,但C++11之后变得十分重要,它是理解 move/forward 等新语义的基础. 左值与 ...
- Android -如何在底部左对齐,中对齐,右对齐三个按钮图片 巧用Relative Layout
Relative Layout 不仅可以指定同级的元素之间的位置关系(e.g. layout_toLeftOf) 还可以指定子元素与父元素之间的位置关系(e.g. layout_alignParent ...
- Android学习——LinearLayout布局实现居中、左对齐、右对齐
android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...
随机推荐
- 在Mac OS 下 build Tesseract4.0 源码并在命令行中使用
作者电脑:Mac Mini 系统信息:OS X EI Capitan 10.11.6 Tesseract4.0github地址:https://github.com/tesseract-ocr/tes ...
- 6-最基础的服务-es6写法
创建server.js 'use strict'; //http模块 var http = require('http'); //封装的方法 var handlers = require('./han ...
- Github 开源:使用升讯威 Mapper( Sheng.Mapper)与 AutoMapper 互补,大幅提高开发效率!
Github 地址:https://github.com/iccb1013/Sheng.Mapper 在上一篇幅中,简单介绍了 升讯威 Mapper( Sheng.Mapper)[http://www ...
- 小兴趣:修改Hosts文件,禁止访问指定网页
不知道Hosts文件什么鬼的朋友可以在网上搜索一下(大牛勿喷- -) 访问网址时,先查询本地的Hosts文件,那么如果我们将Hosts文件中的网址与IP的映射修改之后,将访问错误的IP. 如在文件尾追 ...
- 《Java从入门到放弃》入门篇:springMVC数据传递
springMVC中的数据传递方式与JSP和Struts2相比,更加的简单.具体有什么样的区别呢?我们通过下面这张图来对比就知道了. 随手画的,有些错别字,不用太在意..... 接下来,进入正题,sp ...
- 在django中集成ckeditor富文本
目前用的比较多的富文本插件有百度的ueditor.ckeditor.kindeditor等,其中ueditor和kindeditor比较美观,ckeditor的皮肤较少.但是ueditor加载较慢,k ...
- javascript今生前世
事情得从一个chrome控制台中的无意打印说起. 众所周知,js共六种数据类型,string.number.undefined.boolean.object.null.当然javascript还准备了 ...
- PageObject设计模式,在selenium自动化测试中的运用
PageObject设计模式1. Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 2. web测试时,建议 ...
- ubuntu下apache2-php-mysql的环境配置
基本的支持环境.暂时还不应用zend优化,因此这里就不涉及到zend optimizer的安装了.其实在ubuntu系统中中安装远比在windows系统中设置更为容易,而且在终端下设置更省事. 1.安 ...
- PHP学习之旅——PHP环境搭建
1.wampserver.exe软件下载 http://www.wampsferver.com/官网地址: 选择对应版本下载即可. 2.wampserver服务控制面板 主要控制的是整个wampse ...