自定义 ---UICollectionViewLayout-正N变形居中布局
1. 自定义UICollectionLayout ---- 正三角形居中布局
支持多个图形的自动布局

2. 自定义UICollectionLayout ---- 正方形居中布局

滚动展示的区域

3. 自定义UICollectionLayout ---- 正六边形居中布局(蜂窝布局)

等等正n变形布局,其中 正六边形支持 间距大小的改变,图形的旋转。

旋转 --

间距

4. 核心代码 ---- 绘制正n边形 (贝塞尔曲线)
- (void)layoutSubviews
{
[super layoutSubviews];
// step 1: 绘制正n变形
CGFloat X = self.contentView.frame.size.width * 0.5;
CGFloat Y = self.contentView.frame.size.height * 0.5;
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
bezierPath.lineCapStyle = kCGLineCapRound;
bezierPath.lineJoinStyle = kCGLineCapRound;
CGFloat angle = labs(360/self.number);
[bezierPath moveToPoint: CGPointMake(X - mm , Y)];
for (int i = 0; i < self.number - 1; i ++) {
CGFloat angle1 = angle *(i + 1);
CGFloat XX = X + (mm * (COS(angle1)))*(-1);
CGFloat YY = Y + mm * (SIN(angle1));
[bezierPath addLineToPoint: CGPointMake(XX, YY)];
}
[bezierPath addLineToPoint: CGPointMake(X - mm , Y)];
// step 2: 根据路径生成蒙板
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [bezierPath CGPath];
self.backgroundColor = [UIColor redColor];
// step 3: 给cell添加模版
self.layer.mask = maskLayer;
self.titleLabel.frame = self.bounds;
}
5. 核心代码 --- 自定义Layout布局
- (void)prepareLayout
{
[super prepareLayout];
_itemCount = [self.collectionView numberOfItemsInSection:];
_attributeAttay = [[NSMutableArray alloc]init];
; i < _itemCount; i ++) {
//设置第一个正六边形的中心位置
CGFloat X = self.collectionView.frame.size.width*0.5;
CGFloat Y = self.TopMargin;
== ? -:i%;
== ? :i%;
X += CST(-num)*([self judgeXWithNumber:self.number]);
Y += ([self judgeSizeWithNumber:self.number].height + self.margin)*(i/) + CST(num2)*([self judgeSizeWithNumber:self.number].height/)+self.margin;
UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForRow:i inSection:]];
// 设置每隔item的大小
attribute.size = [self judgeSizeWithNumber:self.number];
// 设置每隔item的圆心
// 计算每个item的中心坐标
attribute.center = CGPointMake(X, Y);
[_attributeAttay addObject:attribute];
}
}
// X轴方向的间隔
- (CGFloat)judgeXWithNumber:(NSInteger)xNumber
{
) {
/xNumber)*mm;
}
)
{
/xNumber)*mm;
}
)
{
/xNumber)*mm - ;
}
)
{
/xNumber)*mm;
}
)
{
/xNumber)*mm;
}
else
{
*mm + self.margin ;
}
}
// 就算正n变形的大小
- (CGSize)judgeSizeWithNumber:(NSInteger)sNumber
{
|| sNumber == ) {
*mm,*SIN(/sNumber)*mm);
}
else
{
*(mm),*mm);
}
}
//设置内容区域的大小
-(CGSize)collectionViewContentSize
{
CGFloat hh = [self judgeSizeWithNumber:self.number].height;
CGFloat height = (hh+self.margin)*(self.itemCount/ + ) + self.TopMargin + ;
if (height > Device_height) {
height = height;
}
else
{
height = Device_height;
}
return CGSizeMake(Device_width, height);
}
//返回设置数组
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return _attributeAttay;
}
View Layout
自定义 ---UICollectionViewLayout-正N变形居中布局的更多相关文章
- 自定义UICollectionViewLayout 布局实现瀑布流
自定义 UICollectionViewLayout 布局,实现瀑布流:UICollectionView和UICollectionViewCell 另行创建,这只是布局文件, 外界控制器只要遵守协议并 ...
- iOS 关于自定义UICollectionViewLayout实现复杂布局
UICollectionView的简单介绍 UICollectionView的结构 Cells Supplementary Views 追加视图 (类似Header或者Footer) Decorati ...
- 自定义UICollectionViewLayout并添加UIDynamic - scorpiozj(转)
转载自:http://www.tuicool.com/articles/jM77Vf 自定义UICollectionViewLayout并添加UIDynamic UICollectionVie ...
- 自定义UICollectionViewLayout并添加UIDynamic
大家也可以到这里查看. UICollectionView是iOS6引入的控件,而UIDynamicAnimator是iOS7上新添加的框架.本文主要涵盖3部分: 一是简单概括UICollectionV ...
- 自定义UICollectionViewLayout 实现瀑布流
今天研究了一下自定义UICollectionViewLayout. 看了看官方文档,要自定义UICollectionViewLayout,需要创建一个UICollectionViewLayout的子类 ...
- 自定义UICollectionViewLayout之CATransform3D
1.自定义UICollectionViewLayout旋转效果 之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D ...
- CSS居中布局总结【转】
居中布局 <div class="parent"> <div class="child">demo</div> </d ...
- CSS居中布局总结
居中布局 <div class="parent"> <div class="child">demo</div> </d ...
- sass—使用自定义function和@each实现栅格布局
/*使用自定义function和@each实现栅格布局*/ @function buildLayout($num: 5){ $map: (defaultValue: 0); //不能直接生成col,需 ...
随机推荐
- 爬虫笔记(十一)——认识cookie
什么是cookie? 在爬虫的使用中,如果涉及登录等操作时,经常会使用到cookie.简单的来说,我们访问每一个互联网页面,都是通过HTTP协议进行的,而HTTP协议是一个无状态协议,所谓的无状态协议 ...
- visual studio code快捷键(mac)
常用快捷键 命令 命令描述 备注 ctl+j 多行文字变成一行 打开首选项->键盘快捷方式->搜索 joinLines: 然后设置你的快捷键再回车即可.
- Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_全局变量的妙用_遍历穿透
1 比如我这边写了一个函数,这个函数中有一个变量作为参数,给定这个参数一个值,然后这个函数返回给我一个值.但是,我写这函数的时候,这个传参我不写到函数里面.可以通过全局变量的方式进行在外部穿透遍历. ...
- js window.onload 加载多个函数和追加函数
平时做项目 经常需要使用window.onload, 用法如下: function func(){alert("this is window onload event!");ret ...
- K - The Unique MST (最小生成树的唯一性)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- vue2.0学习之组件间通信
/* child.vue*/ 子组件 <template> <div> /*必须要用div包裹起来,然后在里面写需要的组件内容,这里面和平常写的html是一样的*/ <d ...
- xml字符串转json字符串
XML字符串转JSON字符串网上的方法很多,这里主要推荐两种: 1.使用org.json包 jar地址:http://mvnrepository.com/artifact/org.json/json ...
- [LC] 437. Path Sum III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- Introduction to Computer Science and Programming in Python--MIT
学习总结--(Introduction to Computer Science and Programming in Python--MIT) 导论 主题 重新利用数据结构来表达知识 理解算法的复杂性 ...
- 双因子方差分析|adjusted R|强度|SSA|SSE|SST|
应用统计学 方差分析的基本假设: 组间组平均与总平均的不同是由treatment引发的.单个值与组平均的不同是由组内error引发的. 如果没有处理误差SSA=SSE,所以右尾假设如果F>1则处 ...