网上的UICollectionView的Layout布局,其cell的形状多为矩形和圆形。

本篇博文将正六边形作为cell的基本形状,为您展现独特的蜂窝布局效果及实现源代码。

帮助您让自己的App脱颖而出,更加与众不同。

最新完整代码下载地址:https://github.com/duzixi/Varied-Layouts

博文首发地址:http://blog.csdn.net/duzixi

实现效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHV6aXhp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" height="300" alt="">

核心源码:

自己定义Layout

//
// HoneyCombLayout.h
// Demo-Layouts
//
// Created by 杜子兮(duzixi) on 14-9-1.
// Copyright (c) 2014年 lanou3g.com All rights reserved.
// #import <UIKit/UIKit.h> @interface HoneyCombLayout : UICollectionViewLayout @property (nonatomic, assign) NSInteger margin;
@property (nonatomic, assign) NSInteger oX;
@property (nonatomic, assign) NSInteger oY; @end
//
// HoneyCombLayout.m
// Demo-Layouts
//
// Created by 杜子兮(duzixi) on 14-9-1.
// Copyright (c) 2014年 lanou3g.com All rights reserved.
// #import "HoneyCombLayout.h" @implementation HoneyCombLayout /// 返回内容大小。用于推断是否须要加快滑动 -(CGSize)collectionViewContentSize
{
float height = (SIZE + self.margin) * ([self.collectionView numberOfItemsInSection:0] / 4 + 1);
return CGSizeMake(320, height);
} /// 返回YES,改变布局
/*
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
*/
#pragma mark - UICollectionViewLayout
/// 为每个Item生成布局特性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; UICollectionView *collection = self.collectionView; float x = (SIZE + self.margin) * (indexPath.item % COL + 1) * 0.75;
float y = (SIZE + self.margin) * (indexPath.item / COL + 0.5) * cos(M_PI * 30.0f / 180.0f);
if (indexPath.item % 2 == 1) {
y += (SIZE + self.margin) * 0.5 * cosf(M_PI * 30.0f / 180.0f);
} x += self.oX;
y += self.oY; attributes.center = CGPointMake(x + collection.contentOffset.x, y + collection.contentOffset.y);
attributes.size = CGSizeMake(SIZE, SIZE * cos(M_PI * 30.0f / 180.0f)); return attributes;
} -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *arr = [super layoutAttributesForElementsInRect:rect];
if ([arr count] > 0) {
return arr;
}
NSMutableArray *attributes = [NSMutableArray array];
for (NSInteger i = 0 ; i < [self.collectionView numberOfItemsInSection:0 ]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
}
return attributes;
} @end

自己定义cell:

//
// HoneycombViewCell.h
// Demo-Layouts
//
// Created by 杜子兮(duzixi) on 14-9-1.
// Copyright (c) 2014年 lanou3g.com All rights reserved.
// #import <UIKit/UIKit.h> @interface HoneycombViewCell : UICollectionViewCell @property (nonatomic,strong) UILabel *titleLabel; @end
//
// HoneycombViewCell.m
// Demo-Layouts
//
// Created by 杜子兮(duzixi) on 14-9-1.
// Copyright (c) 2014年 lanou3g.com All rights reserved.
// #import "HoneycombViewCell.h" @implementation HoneycombViewCell - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.textColor = [UIColor whiteColor];
[self.contentView addSubview:self.titleLabel];
}
return self;
} -(void)layoutSubviews
{
[super layoutSubviews];
// step 1: 生成六边形路径
CGFloat longSide = SIZE * 0.5 * cosf(M_PI * 30 / 180);
CGFloat shortSide = SIZE * 0.5 * sin(M_PI * 30 / 180);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, longSide)];
[path addLineToPoint:CGPointMake(shortSide, 0)];
[path addLineToPoint:CGPointMake(shortSide + SIZE * 0.5, 0)];
[path addLineToPoint:CGPointMake(SIZE, longSide)];
[path addLineToPoint:CGPointMake(shortSide + SIZE * 0.5, longSide * 2)];
[path addLineToPoint:CGPointMake(shortSide, longSide * 2)];
[path closePath]; // step 2: 依据路径生成蒙板
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [path CGPath]; // step 3: 给cell加入模版
self.layer.mask = maskLayer; self.backgroundColor = [UIColor orangeColor];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.frame = self.contentView.frame; } /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/ @end

【iOS】UICollectionView自己定义Layout之蜂窝布局的更多相关文章

  1. iOS开发之蜂窝布局—Swift

    前言 最近项目中用到了类似蜂窝的六边形布局,在这里分享出来抛砖引玉,供大家参考学习.本文提供了2种思路实现效果,第一种方式使用UICollectionView实现,第二种方式使用UIScrollVie ...

  2. iOS UICollectionView(转二)

    UICollectionView的布局是可以自己定义的,在这篇博客中先在上篇博客的基础上进行扩充,我们先使用UICollectionViewFlowLayout,然后好好的介绍一下UICollecti ...

  3. Auto Layout之创建布局约束

    上篇文章给大家介绍了AutoLayout 的由来及OC中关于AutoLayout 的类.这篇文章将向大家介绍,在iOS 编程中怎样使用Auto Layout 构建布局约束. 创建布局约束 创建布局约束 ...

  4. android 分区layout以及虚拟内存布局-小结

    摘要 简述启动过程的内存分配,各个映像的烧写,加载,logo的刷新,文件系统mount. DRAM:外部RAM: ISRAM:内部RAM(128K),(PL会跑在ISRAM里面,去初始化DRAM,lo ...

  5. Android自己定义之流式布局

    流式布局,优点就是父类布局能够自己主动的推断子孩子是不是须要换行,什么时候须要换行,能够做到网页版的标签的效果. 今天就是简单的做了自己定义的流式布局. 详细效果: 原理: 事实上非常easy,Mea ...

  6. ios UICollectionView reloadData无法更新的奇怪问题。

    报错    Assertion failure in -[UICollectionViewData invalidateItemsAtIndexPaths:] 近来偶尔用到UICollectionVi ...

  7. Android Studio分类整理res/Layout中的布局文件(创建子目录)

    res/layout中的布局文件太杂,没有层次感,受不了的我治好想办法解决这个问题. 前几天看博客说可以使用插件分组,可惜我没找到.知道看到另一篇博客时,才知道这个方法不能用了. 不能用插件,那就手动 ...

  8. Best Practices for Performance_3.Improving Layout Performance 优化布局

    http://developer.android.com/training/improving-layouts/index.html 1. 优化布局层次 1)  每增加一个View或者布局,都会增加额 ...

  9. IOS UICollectionView基础+UICollectionViewFlowLayout基础

    UICollectionView在众多控件中也算是比较常用的了,像淘宝在浏览宝贝时采用的就是UICollectionView,对于UICollectionView->UICollectionVi ...

随机推荐

  1. chmod和chown命令具体使用方法

    Linux下数字表示文件的操作权限(777,755,..) Linux下.查看某路径下用(ls -l)查看全部文件的具体属性列表时.会看到文件的操作权限.类似"drwxr-xr-x" ...

  2. mysql设置远程訪问数据库的多种方法

    问题:MySQL权限设置正确,但仍无法远程訪问.通过telnet发现3306port未打开. 分析:MySQL默认仅仅绑定127.0.0.1,即:仅仅有在本机才干訪问3306port. 解决:找到My ...

  3. BAT常问问题总结以及回答(问题汇总篇)

    几个大厂的面试题目目录: java基础(40题)https://www.cnblogs.com/television/p/9397968.html 多线程(51题) 设计模式(8点) JVM(12题) ...

  4. Apache Kylin高级部分之使用Hive视图

    本章节我们将介绍为什么须要在Kylin创建Cube过程中使用Hive视图.而假设使用Hive视图.能够带来什么优点.解决什么样的问题.以及须要学会怎样使用视图.使用视图有什么限制等等. 1.      ...

  5. virtual table(有180个评论)

    To implement virtual functions, C++ uses a special form of late binding known as the virtual table. ...

  6. angularjs1-1

    <!DOCTYPE html> <html> <body> <header> <meta http-equiv="Content-Typ ...

  7. php面向对象之构造函数和析构函数

    php面向对象之构造函数和析构函数 简介 php面向对象支持两种形式的构造函数和析构函数,一种是和类同名的构造函数(php5.5之前),一类是魔术方法(php5.5之后).与类名相同的构造函数优先级比 ...

  8. [jzoj 6092] [GDOI2019模拟2019.3.30] 附耳而至 解题报告 (平面图转对偶图+最小割)

    题目链接: https://jzoj.net/senior/#main/show/6092 题目: 知识点--平面图转对偶图 在求最小割的时候,我们可以把平面图转为对偶图,用最短路来求最小割,这样会比 ...

  9. ROS安装教程

    对于ROS的安装,在它的官方网站: http://wiki.ros.org/ROS/Installation 中也有详细说明.但是对于像博主这样先天英语发育不全的人来说,直接看官网还是有点困难的. 所 ...

  10. docker初安装的血泪史

    最近docker很火,不管是朋友圈内还是公司内聊天都离不开docker,于是对docker产生了极大的好奇心,凭着一颗程序猿的好奇心开始了docker的安装血泪史. 首先我有一台从公司退役的本本x22 ...