• UICollectionView


    UICollectionView简介

    UICollectionView是一种应用广泛的数据展示方式,是iOS6.0以后引进的。在各大app都有广泛的引用。

     
    QQ音乐热搜
     
    下厨房集市页
     
    花瓣的发现

    UICollectionView主要包括了下面几部分:

    1. UICollectionView-内容显示的主视图,类似于UITableView。
    2. UICollectionViewCell-类似于UITableViewCell在UITableView中。这些cell组成了视图中的内容。
    3. Supplementary Views-如果你除了cell之外,还有信息需要展示,你可以使用supplementary views。通常被用作section的header或者footer
    4. Decoration View-装饰视图,用来提升UICollectionView的背景视觉效果的。

    除了上面这些可见的组成部分,UICollectionView还有用来布局内容的不可见组成部分。

    1. UICollectionViewLayout- 用来处理cell在屏幕上的布局。使用一系列的代理方法在UICollectionView上放置cell,而且布局效果是可以在运行时,随时改变的。

    创建UICollectionView的常用方法

    • UICollectionView
      1.创建cell以及header,footer
      使用代码创建
      - registerClass:forCellWithReuseIdentifier:
      - registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
      使用xib创建
      - registerNib:forCellWithReuseIdentifier:
      - registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
      复用cell
      - dequeueReusableCellWithReuseIdentifier:forIndexPath:
      - dequeueReusableSupplementaryViewOfKind:
      :withReuseIdentifier:forIndexPath:

        2.获取Collection View中的Item及位置
        - indexPathForItemAtPoint:
        - indexPathsForVisibleItems
        - indexPathForCell:
        - cellForItemAtIndexPath:
      
        3.获取Collection View的状态
        - numberOfSections
        - numberOfItemsInSection:
      
    • UICollectionViewDelegate
      1.处理选择的Cells
      - collectionView:shouldSelectItemAtIndexPath:
      - collectionView:didSelectItemAtIndexPath:
      - collectionView:shouldDeselectItemAtIndexPath:
      - collectionView:didDeselectItemAtIndexPath:

        2.处理Cells的高亮
        - collectionView:shouldHighlightItemAtIndexPath:
        - collectionView:didHighlightItemAtIndexPath:
        - collectionView:didUnhighlightItemAtIndexPath:
      
    • UICollectionViewDataSource
      1.获取Section和Item的数量
      - collectionView:numberOfItemsInSection:
      - numberOfSectionsInCollectionView:

        2.获取Items的视图
        - collectionView:cellForItemAtIndexPath:
        - collectionView:viewForSupplementaryElementOfKind:
          atIndexPath:
      

    会变魔术的UICollectionViewLayout

    UICollectionView之所以可以有千变万化的样式,各种酷帅屌炸天的动画效果,都要归功于UICollectionViewLayout,玩好的UICollectionViewLayout可以实现你想要的各种效果。

    UICollectionViewLayout会对屏幕上的cells进行组织布局。
    UICollectionViewLayoutAttributes描述了具体如何布局cells, supplementary views, decoration views,具体包括以下属性:

    • frame -view在collection view的坐标系统中的具体位置
    • center -view在collection view的坐标系统中的中心
    • size -view的大小
    • transform3D -用来旋转,缩放的属性
    • zIndex -子视图的层级
    • hidden -view是否隐藏

    布局的生命周期:


     
    来自iOS 6 By Tutotials
    • 蓝框代表colletion view在布局时使用的方法
    • 橙框代表colletion view的状态
    • 绿框代表collection view的使用者调用的方法

    collection view首先调用prepareLayout,在其中设置布局需要的内部数据结构。这时collection view已经弄清了要展示的数据,但还不知道如何布局,所以你可以在prepareLayout中使用numberOfSections或numberOfItemsInSection:这些方法。然后调用collectionViewContentSize获取content size进行布局。之后调用layoutAttributesForElementsInRect
    对可以在屏幕中显示的部分进行布局。layout必须返回屏幕中所有元素的布局属性,包括UICollectionViewCell或者Supplementary Views或者Decoration Views。这时,collectionView了解了足够多的信息去布局cells。当scrollview滑动时,collection view会继续调用layoutAttributesForElementsInRect:去决定新的可见元素的布局。

    当下面三种情况发生时,又会调用布局的相关方法。

    • invalidateLayout-所有布局重新布局
    • insertItemAtIndexPathsdeleteItemsAtIndexPaths
      -这个会发生在用户添加或者移除collection view中的
      item时,这时会调用prepareForCollectionViewUpdate: **
      finalizeAnimatedBoundsChange:**
    • 当collection view的约束改变时 - 调用
      prepareForAnimatedBoundsChange
      finalizeAnimatedBoundsChange

    由于UICollectionViewLayout是抽象类,所以在布局的时候可以使用Apple提供的UICollectionViewFlowLayout或者自定义Layout

    UICollectionViewFlowLayout

    这是一个典型的栅格布局,就像上图所以的下厨房的集市页布局。它有以下的一些属性:

        scrollDrirection:决定了scroll view的滚动方向
        minimumLineSpacing:决定了每行的间隔
        minimumIteriItemSpacing:决定了Item之间的间隔
        itemSize:决定了Item的大小
        sectionInset:section四周的边距
        headerReferenceSize:header的大小
        footerReferenceSize:footer的大小
    
    自定义UICollectionViewLayout:

    自定义UICollectionViewLayout一般是继承UICollectionViewLayout类。重载下列方法:

        -(void)prepareLayout:设定一些初始化参数。
        -(CGSize)collectionViewContentSize:设定Collection View的尺寸
        -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect:返回屏幕中所有元素的布局属性
        -(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath:返回indexPath位置的cell的布局属性
        -(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath:返回indexPath位置的补充视图的布局属性
        -(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath:返回indexPath位置的装饰视图的布局属性
        -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds:边界改变时,是否重新布局。
    

    在下一篇中将介绍几种常用的布局,如开篇的截图所示,栅格网状,靠左对齐,瀑布流的布局。虽然早已有相关轮子,但是作为初学者,模仿轮子,知道轮子的造法,还是很有必要的。

    参考

    1.http://onevcat.com/2012/06/introducing-collection-views/
    2.《iOS 6 By Tutorials》
    3.https://github.com/mokagio/UICollectionViewLeftAlignedLayout
    4.https://github.com/ChenYilong/CollectionViewClassifyMenu
    5.http://www.jianshu.com/p/22adf62ea491



    作者:wright
    链接:https://www.jianshu.com/p/5ccca3696506
    來源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    Farseer.net轻量级开源框架 中级篇:UrlRewriter 地址重写
    Farseer.net轻量级开源框架 中级篇:常用的扩展方法
    Farseer.net轻量级开源框架 中级篇:BasePage、BaseController、BaseHandler、BaseMasterPage、BaseControls基类使用
    Farseer.net轻量级开源框架 中级篇:探究ORM(Mapping)
    Farseer.net轻量级开源框架 中级篇:SQL执行报告
    xamarin ios 和 xcode 版本不匹配 xcode版本过低的问题解决
    xamarin ios for mac 相关包下载 3月17日更新
    Xamarin iOS 第三方绑定库最新爬坑历史
    Xamarin发布ios 详细流程
    9、如何在Xamarin中进行iOS真机调试和发布
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9324480.html
Copyright © 2020-2023  润新知