自定义UICollectionView,主要会用到以下几个方法:

  1. - (void)prepareLayout; 第一次加载layout、刷新layout、以及- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;这个方法返回yes时,会调用。这是苹果官方的说明The collection view calls -prepareLayout once at its first layout as the first message to the layout instance. The collection view calls -prepareLayout again after layout is invalidated and before requerying the layout information. Subclasses should always call super if they override。实现该方法后应该调用[super prepareLayout]保证初始化正确。该方法用来准备一些布局所需要的信息。该方法和init方法相似,但该方法可能会被调用多次,所以一些不固定的计算(比如该计算和collectionView的尺寸相关),最好放在这里,以保证collectionView发生变化时,自定义CollectionView能做出正确的反应。
  2. - (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; 该方法用来返回rect范围内的 cell supplementary 以及 decoration的布局属性layoutAttributes(这里保存着她们的尺寸,位置,indexPath等等),如果你的布局都在一个屏幕内 活着 没有复杂的计算,我觉得这里可以返回全部的属性数组,如果涉及到复杂计算,应该进行判断,返回区域内的属性数组,有时候为了方便直接返回了全部的属性数组,不影响布局但可能会影响性能(如果你的item一屏幕显示不完,那么这个方法会调用多次,当所有的item都加载完毕后,在滑动collectionView时不会调用该方法的)。
  3. - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath; 该方法不是必须实现的,即便你实现了,我们对collectionView的任何操作,也不会导致系统主动调用该方法。该方法通常用来定制某个IndexPath的item的属性。当然我们也可以重写这个方法,将布局时相关的属性设置放在这里,在- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect 或者 - (void)prepareLayout 中 需要创建用来返回给系统的属性数组 主动调用这个方法,并添加带可变数组中去返回给系统。当然我们也可以在 - (void)prepareLayout 中 通过[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] 获取 每个indexPath的attributes,在- (void)prepareLayout中设置所有item的属性。看需求以及个人喜欢。
  4. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds; 用来刷新layout的,当我们返回yes的时候。如果我们的需求不需要实时的刷新layout,那么最好判断newBounds 和 我们的collectionView的bounds是否相同,不同时返回yes;(例如苹果官方的lineLayout,因为每次滑动都要放大item,所以这了就直接返回yes)。

以苹果官方的lineLayout为例,这个是对UICollectionViewFlowLayout的扩充,

  1. -(id)init
  2. {
  3. self = [super init];
  4. if (self) {
  5. self.itemSize = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
  6. self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  7. self.sectionInset = UIEdgeInsetsMake(, 0.0, , 0.0);//上下边距
  8. self.minimumLineSpacing = 50.0;//行间距
  9. }
  10. return self;
  11. }

这里初始化一些信息。

  1. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
  2. {
  3. return YES;
  4. }

因为滑动放大,故这里需要返回yes,实时刷新layout。

  1. -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
  2. {
  3. NSArray* array = [super layoutAttributesForElementsInRect:rect];
  4.  
  5. //可视rect
  6. CGRect visibleRect;
  7. visibleRect.origin = self.collectionView.contentOffset;
  8. visibleRect.size = self.collectionView.bounds.size;
  9.  
  10. //设置item的缩放
  11. for (UICollectionViewLayoutAttributes* attributes in array) {
  12. if (CGRectIntersectsRect(attributes.frame, rect)) {
  13. CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;//item到中心点的距离
  14. CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;//距离除以有效距离得到标准化距离
  15. //距离小于有效距离才生效
  16. NSLog(@"%f",distance);
  17. if (ABS(distance) < ACTIVE_DISTANCE) {
  18. CGFloat zoom = + ZOOM_FACTOR*( - ABS(normalizedDistance));//缩放率范围1~1.3,与标准距离负相关
  19. attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);//x,y轴方向变换
  20. //attributes.zIndex = 0;
  21. }
  22. }
  23. }
  24.  
  25. return array;
  26. }

这里对item进行放大操作(因为该类是继承自UICollectionViewFlowLayout,苹果重写了该方法,所以调用super可以拿到当前rect范围内attributes,如果继承自UICollectionViewLayout,调用super是什么都拿不到的)。这里的思想就是:距离屏幕中心超过一定距离(假设200,自己设定)开始放大,CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;item中心到屏幕中心点距离占200的比例,

  1. CGFloat zoom = 1 + ZOOM_FACTOR*(1 - ABS(normalizedDistance));

这样便得到放大的倍数。

  1. - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
  2. {
  3. //proposedContentOffset是没有对齐到网格时本来应该停下的位置
  4.  
  5. //计算出实际中心位置
  6. CGFloat offsetAdjustment = MAXFLOAT;
  7. CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
  8.  
  9. //取当前屏幕中的UICollectionViewLayoutAttributes
  10. CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
  11. NSArray* array = [super layoutAttributesForElementsInRect:targetRect];
  12.  
  13. //对当前屏幕中的UICollectionViewLayoutAttributes逐个与屏幕中心进行比较,找出最接近中心的一个
  14. for (UICollectionViewLayoutAttributes* layoutAttributes in array) {
  15. CGFloat itemHorizontalCenter = layoutAttributes.center.x;
  16. if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
  17. offsetAdjustment = itemHorizontalCenter - horizontalCenter;
  18. }
  19. }
  20.  
  21. //返回调整好的point
  22. return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
  23. }

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity 这个方法简单理解可以当作是用来设置collectionView的偏移量的,计算当前屏幕哪个item中心点距离屏幕中心点近,就将该item拉到中心去。

个人理解,不对的地方还请见谅。

关于自定义UICollectionViewLayout的一点个人理解<一>的更多相关文章

  1. 自定义UICollectionViewLayout并添加UIDynamic - scorpiozj(转)

    转载自:http://www.tuicool.com/articles/jM77Vf     自定义UICollectionViewLayout并添加UIDynamic UICollectionVie ...

  2. 自定义UICollectionViewLayout并添加UIDynamic

    大家也可以到这里查看. UICollectionView是iOS6引入的控件,而UIDynamicAnimator是iOS7上新添加的框架.本文主要涵盖3部分: 一是简单概括UICollectionV ...

  3. 自定义UICollectionViewLayout 实现瀑布流

    今天研究了一下自定义UICollectionViewLayout. 看了看官方文档,要自定义UICollectionViewLayout,需要创建一个UICollectionViewLayout的子类 ...

  4. 自定义UICollectionViewLayout 布局实现瀑布流

    自定义 UICollectionViewLayout 布局,实现瀑布流:UICollectionView和UICollectionViewCell 另行创建,这只是布局文件, 外界控制器只要遵守协议并 ...

  5. net core体系-web应用程序-4net core2.0大白话带你入门-8asp.net core 内置DI容器(DependencyInjection,控制翻转)的一点小理解

    asp.net core 内置DI容器的一点小理解   DI容器本质上是一个工厂,负责提供向它请求的类型的实例. .net core内置了一个轻量级的DI容器,方便开发人员面向接口编程和依赖倒置(IO ...

  6. AngularJS』一点小小的理解

    『AngularJS』一点小小的理解   AngularJS 是一个前端的以Javascript为主的MVC框架.与AngularJS相类似的还有EmberJS. 随着时代在进步,各种各样的开发理念与 ...

  7. iOS 关于自定义UICollectionViewLayout实现复杂布局

    UICollectionView的简单介绍 UICollectionView的结构 Cells Supplementary Views 追加视图 (类似Header或者Footer) Decorati ...

  8. 自定义UICollectionViewLayout之CATransform3D

    1.自定义UICollectionViewLayout旋转效果 之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D ...

  9. Layui的一点小理解(上)

    首先声明这是个人的一点理解,如有不对之处请指正,以下的例子有在官网上看到的,有的是自己写的.还是老规矩最后会附上官网的,如有不明白之处,请查看文档或留言. 既然说Layui,当然要简单的介绍以下什么是 ...

随机推荐

  1. 解决linux下oracle进入sqlplus环境中后退键显示^H、上下键无效与ctrl+l无法清屏等问题【weber出品必属精品】

    习惯linux中上下键回退历史输入的人们肯定也希望sqlplus中也能实现相同的效果,可是不幸的是,sqlplus中不提供诸多方便的快捷键,这个时候我们就需要另外安装一个插件来实现这个想法. 这个插件 ...

  2. 一个cocos2d-x的项目

    前几天完成了一个cocos2d-x的项目,放在git上: https://github.com/gittor/Jigsaw 采用cocos的版本是3.7.1. 项目是一个拼图的游戏,市面上的拼图类游戏 ...

  3. 利用JS实现HTML TABLE的分页

    有时候table的列数太长,不利于使用者查询,所以利用JS做了一个table的分页,以下为相关代码 一.JS代码 <script type="text/javascript" ...

  4. Autolayout-VFL语言添加约束-备

    一.VFL语言简介 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...

  5. libc++abi.dylib: terminate_handler unexpectedly threw an exception错误小结

    说法一: 我们在运行xcode工程时,有时候会遇到”libc++abi.dylib: terminate_handler unexpectedly threw an exception”错误,app莫 ...

  6. 【给你几个使用Xamarin的理由】

    写在开篇前 这种代理操作,绑定影射的机制,有些极端的开发者确实难以接受.追求完美,总感觉原生的各种优点. 如果你非得较这个真,那您还是感觉补习下 Java Eclipse  ,买一台Mac 恶补Obj ...

  7. 理解class.forName()(转)

    使用jdbc方式连接数据库时会使用一句代码Class.forName(String className).这句话是什么意思呢?首先说一点Class.forName(String className)这 ...

  8. 图解SSL/TLS协议(转)

    本周,CloudFlare宣布,开始提供Keyless服务,即你把网站放到它们的CDN上,不用提供自己的私钥,也能使用SSL加密链接. 我看了CloudFlare的说明(这里和这里),突然意识到这是绝 ...

  9. hdr(host), hdr_beg(host) , path_beg

    ACL derivatives : hdr([<name>[,<occ>]]) : exact string match 字符串精确匹配 hdr_beg([<name&g ...

  10. geoip 添加一列,add_field =>["[geoip][request_time]","%{request_time}"]

    "message" => " 10.171.246.184 [11/Sep/2016:14:42:53 +0800] \"GET /wechat/home ...