• iOS---UICollectionView详解和常用API翻译


    UICollectionView

    • 1.必须要设置布局参数
    • 2.注册cell
    用法类似于UITableView 类。自动实现重用,必须注册初始化。
    使用UICollectionView必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

    Collection View的构成,我们能看到的有三个部分:

    • Cells
    • Supplementary Views 追加视图 (类似Header或者Footer)
    • Decoration Views 装饰视图 (用作背景展示)

    一、UICollectionViewLayout

    • UICollectionView的精髓
    • Layout决定了UICollectionView是如何显示在界面上的。在展示之前,一般需要生成合适的UICollectionViewLayout子类对象,并将其赋予CollectionView的collectionViewLayout属性。

    1.UICollectionViewFlowLayout

    • 最简单也是最常用的默认layout对象,UICollectionViewFlowLayout。Flow Layout简单说是一个直线对齐的layout,

    常用属性

    // 行间距,也可以通过collectionView: layout:minimumLineSpacingForSectionAtIndex:
    @property (nonatomic) CGFloat minimumLineSpacing;
    
    // 设置cell之间的间距
    @property (nonatomic) CGFloat minimumInteritemSpacing;
    
    // 定义每一个item的大小。通过设定itemSize可以全局地改变所有cell的尺寸,如果想要对某个cell制定尺寸,
    //可以使用-collectionView:layout:sizeForItemAtIndexPath:方法。
    @property (nonatomic) CGSize itemSize;
    
    @property (nonatomic) CGSize estimatedItemSize
    
    // 滚动方向,默认是水平
    // UICollectionViewScrollDirectionVertical
    // UICollectionViewScrollDirectionHorizontal
    @property (nonatomic) UICollectionViewScrollDirection scrollDirection;
    
    // 根据滚动方向不同,header和footer的高和宽中只有一个会起作用。
    // 垂直滚动时section间宽度为该尺寸的高,而水平滚动时为宽度起作用,
    @property (nonatomic) CGSize headerReferenceSize;
    @property (nonatomic) CGSize footerReferenceSize;
    
    // 组间距 缩进
    @property (nonatomic) UIEdgeInsets sectionInset;
    

    UICollectionViewDelegateFlowLayout代理方法

    // 设定指定Cell的尺寸
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
    
    // 设定collectionView(指定区)的边距
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
    
    // 设定指定区内Cell的最小行距,也可以直接设置UICollectionViewFlowLayout的minimumLineSpacing属性
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
    
    // 设定指定区内Cell的最小间距,也可以直接设置UICollectionViewFlowLayout的minimumInteritemSpacing属性
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
    

    2.UICollectionViewLayoutAttributes

    • 一个非常重要的类,

    属性列表

    // 边框
    @property (nonatomic) CGRect frame
    // 中心点
    @property (nonatomic) CGPoint center
    // 大小
    @property (nonatomic) CGSize size
    // 形状
    @property (nonatomic) CATransform3D transform3D
    // 透明度
    @property (nonatomic) CGFloat alpha
    // 层次关系
    @property (nonatomic) NSInteger zIndex
    // 是否隐藏
    @property (nonatomic, getter=isHidden) BOOL hidden
    

    3.自定义的UICollectionViewLayout

    • UICollectionViewLayout的功能为向UICollectionView提供布局信息.
    • 继承UICollectionViewLayout类。

    重写方法

    // 返回collectionView的内容的尺寸
    -(CGSize)collectionViewContentSize
    
    // 返回rect中的所有的元素的布局属性
    /*
    返回的是包含UICollectionViewLayoutAttributes的NSArray
    UICollectionViewLayoutAttributes可以是cell,追加视图或装饰
    视图的信息,通过不同的UICollectionViewLayoutAttributes初始
    化方法可以得到不同类型的UICollectionViewLayoutAttributes:
    */
    -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    
    // 返回对应于indexPath的位置的cell的布局属性
    -(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath
    
    //返回对应于indexPath的位置的追加视图的布局属性,如果没有追加视图可不重载
    -(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath
    
    // 返回对应于indexPath的位置的装饰视图的布局属性,如果没有装饰视图可不重载
    -(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath
    
    // 当边界发生改变时,是否应该刷新布局。如果YES则在边界变化(一般是scroll到其他地方)时,将重新计算需要的布局信息。
    -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
    
    • 另外需要了解的是,在初始化一个UICollectionViewLayout实例后,会有一系列准备方法被自动调用,以保证layout实例的正确。

    • 首先,-(void)prepareLayout将被调用,

      • 默认下该方法什么没做,但是在自己的子类实现中
      • ,一般在该方法中设定一些必要的layout的结构和初始需要的参数等。
    • 之后,-(CGSize) collectionViewContentSize将被调用,

      • 以确定collection应该占据的尺寸。注意这里的尺寸不是指可视部分的尺寸,而应该是所有内容所占的尺寸。
      • collectionView的本质是一个scrollView,因此需要这个尺寸来配置滚动行为。

    二、UICollectionView

    1.UICollectionViewDataSource

    ①section的数量 -numberOfSectionsInCollection:
    ②某个section里有多少个item -collectionView:numberOfItemsInSection:
    ③对于某个位置应该显示什么样的cell -collectionView:cellForItemAtIndexPath:

    2.UICollectionViewDelegate

    // 当指定indexPath处的item被选择时触发
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    
    //返回这个UICollectionView是否可以被选择
    -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
    
    // 下面是三个和高亮有关的方法:
    
    - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
    
    - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
    
    - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
    
    
    /*
    事件的处理顺序如下:
    
    1.手指按下
    2.shouldHighlightItemAtIndexPath (如果返回YES则向下执行,否则执行到这里为止)
    3.didHighlightItemAtIndexPath (高亮)
    4.手指松开
    5.didUnhighlightItemAtIndexPath (取消高亮)
    6.shouldSelectItemAtIndexPath (如果返回YES则向下执行,否则执行到这里为止)
    7.didSelectItemAtIndexPath (执行选择事件)
    */
    
  • 相关阅读:
    监听 view 初始化时
    动画 帧动画
    Android Studio 修改jar包中的.class代码
    ScrollView 滚动条
    SpannableString 设置文字中的颜色 链接
    onActivityResult 解耦 不需要一层一层的写
    滚动 Scroller OverScroller
    startActivity 新开一个Activity
    Fragment java.lang.IllegalStateException 生命周期
    jenkins 基于docker部署springboot项目详细步骤
  • 原文地址:https://www.cnblogs.com/ShaoYinling/p/4766461.html
Copyright © 2020-2023  润新知