zoukankan      html  css  js  c++  java
  • UICollectionViewLayout详解,文档翻译

    实现一个UICollectionView,和UITableView类似,不过初始化的时候要传入一个UICollectionViewLayout。

    • 苹果给UIcollectionview中的所有视图都来自一个可重用的基类,就是UICollectionReusableView。意思就是UIcollectionViewCell和自定义SupplementaryView和DecorationView都是继承者这个UICollectionReusableView类来构建自己的视图。

    • UICollectionViewLayout是UICollectionView最精髓的部分,主要用来为collection view的元素提供位置信息和状态信息。UICollectionViewLayout并不负责创建视图,这些视图是collection view的数据源(data source)创建的。布局对象只不过定义了这些视图的位置和大小。有了它我们可以自定义Cell的布局,苹果为我们设计了一套非常灵活通用的Layout子类UICollectionViewFlowLayout。许多大神在博客上已经对其进行了详细讲解,本篇主要是记录我对UICollectionViewFlowLayout的理解。

    • 需要注意的是,collection view进行的布局限制在屏幕可见的范围之内。(注:即,即便创建了所有元素的布局对象,但是真正的布局只是在可见的范围内,超出屏幕的部分没有布局

    UICollectionViewLayoutAttributes 布局属性

    该属性可以拿到每个Cell、SupplementaryView、DecorationView的布局属性,反过来,collection view 使用该属性将cell和SupplementaryView放在其边界内。

    CGRect frame 布局视图的frame简单明了
    CGPoint center 视图中心点
    CGSize size 视图尺寸
    CATransform3D transform3D 这个属性可以用来做酷炫的3D动画
    CGAffineTransform transform 转场属性
    CGFloat alpha 透明度
    NSInteger zIndex 层级,数字越大,层级越高(最上面)。
    NSIndexPath *indexPath 如果是cell有对应的indexPath
    UICollectionElementCategory representedElementCategory 视图标记,是cell还是supplementary View或者decoration View
    registerClass:forDecorationViewOfKind: 注册decoration View
    registerNib:forDecorationViewOfKind:
    +(instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath *)indexPath 这个类方法是decoration View布局的来源
    -(nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath 与上一个方法结合得到decoration View布局
    

    UICollectionViewLayoutInvalidationContext

    当UICollectionView调用invalidateLayoutWithContext:方法重新布局的时候配置该参数,指定哪些cell需要重新布局

    UICollectionViewLayout

    该类指定UICollectionView中每个元素的布局
    UICollectionViewLayout有3个分类,它们的大概作用如下:

    • @interface UICollectionViewLayout (UISubclassingHooks) 这个扩展类中,都是用来布局UIcollectionView子视图的
    • @interface UICollectionViewLayout (UIUpdateSupportHooks) 用来布局删除插入动作
    • @interface UICollectionViewLayout (UIReorderingSupportHooks) 移动动作布局

    1、UICollectionViewLayout (UISubclassingHooks)这个分类主要做布局,在这个分类里需要重写的方法 :

    
    // 1、准备布局,布局初始化一般在这里进行
    - (void)prepareLayout;
    
    // 每当collectionView边界改变时便调用这个方法询问 是否 重新初始化布局 是则调用prepareLayout准备布局
    - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;
    
    // 2、初始化布局时调用,返回特定rect(有可能是也可能不是可见rect),每个cell和Supplementary和Decoration的布局属性
    - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect: (CGRect)rect;
    
    // 当滚动停止时,会调用该方法确定collectionView滚动到的位置
    - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity;
    //在使用UICollectionViewFlowLayout布局的时候,有时会有特别的需求,比如:
    //当一个cell滑动到屏幕中点的时候放大,并且当我停止滑动时,能够将离屏幕最近的cell居中。
    //这四个方法就能轻松的完成这样的事。
    
    
    
    
    
    - (CGSize)collectionViewContentSize;
    
    返回collectionView内容区的宽度和高度,子类必须重载该方法,
    返回值代表了所有内容的宽度和高度,而不仅仅是可见范围的,
    collectionView通过该信息配置它的滚动范围,默认返回 CGSizeZero。
    
    
    - (NSArray<__kindofUICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;
    返回UICollectionViewLayoutAttributes 类型的数组,
    UICollectionViewLayoutAttributes 对象包含cell或view的布局信息。
    子类必须重载该方法,并返回该区域内所有元素的布局信息,包括cell,追加视图和装饰视图。
    
    在创建UICollectionViewLayoutAttributes的时候,创建的是相应元素类型(cell, supplementary, decoration)的 attributes对象,比如:
    + (instancetype)layoutAttributesForCellWithIndexPath:(NSIndexPath *)indexPath;
    + (instancetype)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind withIndexPath:(NSIndexPath *)indexPath;
    + (instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind withIndexPath:(NSIndexPath *)indexPath;
    UICollectionView 根据不同的类型区分属性,并根据这些信息决定创建怎样的视图及如何进行管理。
    
    
    -(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
    返回指定indexPath的item的布局信息。子类必须重载该方法,该方法只能为cell提供布局信息,不能为补充视图和装饰视图提供。
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
    如果你的布局支持追加视图的话,必须重载该方法,该方法返回的是追加视图的布局信息,
    kind这个参数区分段头还是段尾的,在collectionview注册的时候回用到该参数。
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath;
    如果你的布局支持装饰视图的话,必须重载该方法,该方法返回的是装饰视图的布局信息,
    ecorationViewKind这个参数在collectionview注册的时候回用到
    
    - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
    该方法用来决定是否需要更新布局。如果collection view需要重新布局返回YES,否则返回NO,默认返回值为NO。
    子类重载该方法的时候,基于是否collectionview的bounds的改变会引发cell和view布局的改变(滚动屏幕cell或者view会不会变),给出正确的返回值。
    如果返回YES,UICollectionView通过调用invalidateLayoutWithContext方法使原来的layout失效
    
    这些方法为collection view 在屏幕上布局提供了最基础的布局信息,如果你不想为追加视图和装饰视图布局,可以不去重载相应的方法。
    

    2、UICollectionViewLayout (UIUpdateSupportHooks)这个分类主要做删除插入等动作,

    • 当collection view的数据发生改变的时候,比如插入或者删除 item的时候,collection view将会要求布局对象更新相应的布局信息。添加、删除 items时都必须更新相应的布局信息以便反映元素最新的位置。
    • 而collection view删除或者添加元素的时候,将会调用一些不同的方法,你应该重载以便提供正确的布局信息.
      在这个分类里需要重写的方法 :
    
    - (void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems;
    做一些和布局相关的准备工作。该方法在插入删除动作之前被调用
    
    
    
      /**  一般来说,我们对布局属性从初始状态到结束状态进行线性插值来计算 collection view 的动画参数。
        然而,新插入或者删除的元素并没有最初或最终状态来进行插值。
        要计算这样的 cells 的动画,collection view 将通过 initialLayoutAttributesForAppearingItemAtIndexPath:
         以及 finalLayoutAttributesForAppearingItemAtIndexPath: 方法来询问其布局对象,
        以获取最初的和最后的属性。
        下面这两个方法是成对出现的,一个是在在屏幕上出现之前调用,一个是在屏幕上出现之后调用
        在[collecView reloadData]或者performBatchUpdates:completion:调用的时候
        只要是有刷新的效果,他们就会被调用多次,视图不断的消失(失效,被摧毁)出现(重组 被创建或者回收利用)*/
    
    - (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
    //在一个 item被插入到collection view 的时候,返回开始的布局信息。
    //这个方法在 prepareForCollectionViewUpdates:之后和finalizeCollectionViewUpdates 之前调用。
    //UICollectionView将会使用该布局信息作为动画的起点(结束点是该item在UICollectionView的最新的位置)。
    //如果返回为nil,布局对象将用item的最终的attributes 作为动画的起点和终点。
    
    - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
    //返回值是item即将从collection view移除时候的布局信息,对即将删除的item来讲,
    //该方法在 prepareForCollectionViewUpdates: 之后和finalizeCollectionViewUpdates 之前调用。
    //在该方法中返回的布局信息描包含 item的状态信息和位置信息。
    //UICollectionView将会把该信息作为动画的终点(起点是item当前的位置)。
    //如果返回为nil的话,布局对象将会把当前的attribute,作为动画的起点和终点。
    
    也可以重载
    - (void)finalizeCollectionViewUpdates;
    通过该方法添加一些动画到block,或者做一些和最终布局相关的工作。更新结束后调用
    
    //返回值是追加视图插入UICollectionView时的布局信息。以下方法使用同initialLayoutAttributesForAppearingItemAtIndexPath:
    - (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
    //返回值是装饰视图插入UICollectionView时的布局信息。
    - (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
    //返回值是追加视图删除UICollectionView时的布局信息。
    - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
    //返回值是装饰视图删除UICollectionView时的布局信息。
    - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath
    
    
    
    
    

    举例:demo下载(https://github.com/mpospese/CircleLayout)

    demo中当调用
    [self.collectionView performBatchUpdates:^{
      [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:tappedCellPath]];  
      } completion:nil];
    方法删除cell的时候,在UICollectionViewLayout中,以下方法会被先后调用:
    1、- (void)prepareForCollectionViewUpdates:(NSArray<UICollectionViewUpdateItem *> *)updateItems;
    2、- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
    3、- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
    4、- (void)finalizeCollectionViewUpdates
    其中2、3方法会反复调用多次。视图不断的消失(失效,被摧毁)出现(重组 被创建或者回收利用)
    

    ** 3、UICollectionViewLayout (UIReorderingSupportHooks) 移动动作布局**

    • 对于移动的元素,UICollectionView提供了标准的方法获取更新后的布局信息。
    - (NSIndexPath *)targetIndexPathForInteractivelyMovingItem:(NSIndexPath *)previousIndexPath withPosition:(CGPoint)position NS_AVAILABLE_IOS(9_0);
    根据item在UICollectionView中的位置获取该item的NSIndexPath。
    第一个参数该item原来的index path,第二个参数是item在collection view中的位置。
     在item移动的过程中,该方法将UICollectionView中的location映射成相应 NSIndexPaths。该方法的默认是显示,查找指定位置的已经存在的cell,返回该cell的NSIndexPaths 。如果在相同的位置有多个cell,该方法默认返回最上层的cell。
    
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForInteractivelyMovingItemAtIndexPath:(NSIndexPath *)indexPath withTargetPosition:(CGPoint)position NS_AVAILABLE_IOS(9_0);
    当item在手势交互下移动时,通过该方法返回这个item布局的attributes 。
    默认实现是,复制已存在的attributes,改变attributes两个值,一个是中心点center;另一个是z轴的坐标值,设置成最大值。
    所以该item在collection view的最上层。子类重载该方法,可以按照自己的需求更改attributes,
    首先需要调用super类获取attributes,然后自定义返回的数据结构。
    
    - (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItems:(NSArray<NSIndexPath *> *)targetIndexPaths withTargetPosition:(CGPoint)targetPosition previousIndexPaths:(NSArray<NSIndexPath *> *)previousIndexPaths previousPosition:(CGPoint)previousPosition NS_AVAILABLE_IOS(9_0);
    - (UICollectionViewLayoutInvalidationContext *)invalidationContextForEndingInteractiveMovementOfItemsToFinalIndexPaths:(NSArray<NSIndexPath *> *)indexPaths previousIndexPaths:(NSArray<NSIndexPath *> *)previousIndexPaths movementCancelled:(BOOL)movementCancelled NS_AVAILABLE_IOS(9_0);
    

    UICollectionViewFlowLayout流水布局

    它继承自UICollectionViewLayout。


    属性.png

    基本都是一看就知道它是干嘛的。
    但是如果每个cell布局样式都不一样的话,就要在代理方法里面设置这些属性

    通过代理设置.png

    也是顾名思义的。在子类化UICollectionViewLayout之前,应该先了解UICollectionViewFlowLayout,看看它是否已经能够满足你的布局需求。

    参考:http://blog.csdn.net/qq_30513483/article/details/51611653



    作者:丶丶夏天
    链接:https://www.jianshu.com/p/52336aa9fc2b
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    Web Workers 的基本信息
    关于前端框架的一些观点
    解密jQuery内核 DOM操作方法(二)html,text,val
    解密jQuery内核 DOM操作
    解密jQuery内核 DOM操作的核心buildFragment
    解密jQuery内核 DOM操作的核心函数domManip
    前端MVC框架Backbone 1.1.0源码分析(二)
    前端MVC框架Backbone 1.1.0源码分析(一)
    解密jQuery内核 Sizzle引擎筛选器
    解密jQuery事件核心
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/11314450.html
Copyright © 2011-2022 走看看