UICollectionViewLayout Apple 官方API (一)

Apple 的 API 方便自己查阅....

extension UICollectionView {
    // 表示集合视图的分区头部的元素种类,常用于注册或者重用头部视图
    @available(iOS 6.0, *)
    public class let elementKindSectionHeader: String

    // 表示集合视图的分区尾部的元素种类,常用于注册或者重用尾部视图
    @available(iOS 6.0, *)
    public class let elementKindSectionFooter: String

    // 表示集合视图的滚动方向:垂直或水平
    public enum ScrollDirection : Int, @unchecked Sendable {
        case vertical = 0
        case horizontal = 1
    }

    // 表示集合视图的元素类别:单元格、补充视图(如头部和尾部视图)、装饰视图(如背景视图)
    public enum ElementCategory : UInt, @unchecked Sendable {
        case cell = 0
        case supplementaryView = 1
        case decorationView = 2
    }
}

// 布局属性,决定了集合视图的每个元素(单元格、补充视图、装饰视图)的显示样式和位置等属性
@available(iOS 6.0, *)
@MainActor open class UICollectionViewLayoutAttributes : NSObject, NSCopying, UIDynamicItem {
    // 视图的 frame,位置和大小
    open var frame: CGRect

    // 视图的 center,中心点
    open var center: CGPoint

    // 视图的 size,大小
    open var size: CGSize

    // 视图的 transform3D,3D 变换
    open var transform3D: CATransform3D

    // 视图的 bounds,边界
    @available(iOS 7.0, *)
    open var bounds: CGRect

    // 视图的 transform,2D 变换
    @available(iOS 7.0, *)
    open var transform: CGAffineTransform

    // 视图的 alpha,透明度
    open var alpha: CGFloat

    // 视图的 zIndex,堆叠顺序,数字大的在上面
    open var zIndex: Int 

    // 视图的 isHidden,是否隐藏
    open var isHidden: Bool 

    // 视图的 indexPath,位置信息
    open var indexPath: IndexPath

    // 代表的元素类别
    open var representedElementCategory: UICollectionView.ElementCategory { get }

    // 代表的元素种类,如果代表的元素类别是单元格,则为 nil
    open var representedElementKind: String? { get }

    // 构造方法,为单元格创建布局属性
    public convenience init(forCellWith indexPath: IndexPath)

    // 构造方法,为补充视图创建布局属性
    public convenience init(forSupplementaryViewOfKind elementKind: String, with indexPath: IndexPath)

    // 构造方法,为装饰视图创建布局属性
    public convenience init(forDecorationViewOfKind decorationViewKind: String, with indexPath: IndexPath)
}

// 布局无效上下文,当集合视图的布局需要更新时,会创建这个对象,其中包含了需要更新的信息
@available(iOS 7.0, *)
@MainActor open class UICollectionViewLayoutInvalidationContext : NSObject {
    // 无效化所有内容,当调用了 reloadData 时,这个属性会为 YES
    open var invalidateEverything: Bool { get }

    // 无效化数据源的数量,当调用了 reloadData 或者插入或删除了项目时,这个属性会为 YES
    open var invalidateDataSourceCounts: Bool { get }

    // 无效化某些项目
    @available(iOS 8.0, *)
    open func invalidateItems(at indexPaths: [IndexPath])

    // 无效化某些补充元素
    @available(iOS 8.0, *)
    open func invalidateSupplementaryElements(ofKind elementKind: String, at indexPaths: [IndexPath])

    // 无效化某些装饰元素
    @available(iOS 8.0, *)
    open func invalidateDecorationElements(ofKind elementKind: String, at indexPaths: [IndexPath])

    // 返回被无效化的项目的索引路径
    @available(iOS 8.0, *)
    open var invalidatedItemIndexPaths: [IndexPath]? { get }

    // 返回被无效化的补充元素的索引路径
    @available(iOS 8.0, *)
    open var invalidatedSupplementaryIndexPaths: [String : [IndexPath]]? { get }

    // 返回被无效化的装饰元素的索引路径
    @available(iOS 8.0, *)
    open var invalidatedDecorationIndexPaths: [String : [IndexPath]]? { get }

    // 内容偏移的调整量,默认为 CGPointZero
    @available(iOS 8.0, *)
    open var contentOffsetAdjustment: CGPoint 

    // 内容大小的调整量,默认为 CGSizeZero
    @available(iOS 8.0, *)
    open var contentSizeAdjustment: CGSize 
}

// 集合视图的布局对象,负责管理集合视图中所有元素的布局
@available(iOS 6.0, *)
@MainActor open class UICollectionViewLayout : NSObject, NSCoding {
    // 构造方法
    public init()

    // 构造方法,从编码器中初始化
    public init?(coder: NSCoder)

    // 集合视图对象
    open var collectionView: UICollectionView? { get }

    // 无效化布局,表示需要重新获取布局信息
    open func invalidateLayout()

    // 无效化布局,使用特定的无效化上下文
    @available(iOS 7.0, *)
    open func invalidateLayout(with context: UICollectionViewLayoutInvalidationContext)

    // 注册装饰视图的类
    open func register(_ viewClass: AnyClass?, forDecorationViewOfKind elementKind: String)

    // 注册装饰视图的 nib
    open func register(_ nib: UINib?, forDecorationViewOfKind elementKind: String)
}

// UICollectionViewLayoutAttributes 是一个对象,用来管理集合视图中所有元素的布局信息。
@available(iOS 6.0, *)
@MainActor open class UICollectionViewLayoutAttributes : NSObject, NSCopying, UIDynamicItem {

    // frame 描述了视图在集合视图的位置和大小
    open var frame: CGRect

    // center 定义了视图的中心点
    open var center: CGPoint

    // size 用来设置视图的宽度和高度
    open var size: CGSize

    // transform3D 用于3D变换,例如旋转或缩放视图
    open var transform3D: CATransform3D

    // bounds 用于描述视图的位置和大小,但是相对于其自身坐标系统而不是集合视图
    @available(iOS 7.0, *)
    open var bounds: CGRect

    // transform 用于2D变换,例如旋转或缩放视图
    @available(iOS 7.0, *)
    open var transform: CGAffineTransform

    // alpha 用于设置视图的透明度
    open var alpha: CGFloat

    // zIndex 用于设置视图在层叠顺序中的位置
    open var zIndex: Int // default is 0

    // isHidden 可以用来隐藏视图
    open var isHidden: Bool // As an optimization, UICollectionView might not create a view for items whose hidden attribute is YES

    // indexPath 是一个独特的标识符,用于确定集合视图中特定的项目或补充视图
    open var indexPath: IndexPath
}

// UICollectionViewLayoutInvalidationContext 是一个对象,它定义了布局何时需要被更新以及哪些部分需要被更新。
@available(iOS 7.0, *)
@MainActor open class UICollectionViewLayoutInvalidationContext : NSObject {

    // invalidateEverything 表示是否需要刷新整个布局
    open var invalidateEverything: Bool // set to YES when invalidation occurs because the collection view is sent -reloadData

    // invalidateDataSourceCounts 表示是否需要重新查询集合视图的项目数量和部分数量
    open var invalidateDataSourceCounts: Bool // if YES, the layout should requery section and item counts from the collection view - set to YES when the collection view is sent -reloadData and when items are inserted or deleted

    // 以下方法用于标记需要重新计算布局的项和视图
    @available(iOS 8.0, *)
    open func invalidateItems(at indexPaths: [IndexPath])

    @available(iOS 8.0, *)
    open func invalidateSupplementaryElements(ofKind elementKind: String, at indexPaths: [IndexPath])

    @available(iOS 8.0, *)
    open func invalidateDecorationElements(ofKind elementKind: String, at indexPaths: [IndexPath])
}

// UICollectionViewLayout 是所有集合视图布局类的基类。
@available(iOS 6.0, *)
@MainActor open class UICollectionViewLayout : NSObject, NSCoding {

    // collectionView 属性返回使用此布局对象的集合视图
    open var collectionView: UICollectionView? { get }

    // invalidateLayout 方法用于标记当前布局需要重新计算
    open func invalidateLayout()

    // register 方法用于注册用于装饰视图的类或nib
    open func register(_ viewClass: AnyClass?, forDecorationViewOfKind elementKind: String)
    open func register(_ nib: UINib?, forDecorationViewOfKind elementKind: String)

    // prepare 方法在布局对象第一次布局和布局无效之后被集合视图调用,以便布局对象可以更新其内部状态
    open func prepare()

    // 以下方法返回特定元素的布局属性
    open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
    open func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
    open func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?

    // shouldInvalidateLayout(forBoundsChange:) 方法决定了当集合视图的边界发生改变时,是否需要重新计算布局
    open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool // return YES to cause the collection view to requery the layout for geometry information
}

extension UICollectionViewLayout {
    // 在集合视图有删除/插入的更新时调用此方法。
    // 该方法会在调用下面的初始/最终布局属性方法之前被调用,以便布局有机会批量计算插入和删除的布局属性。
    // updateItems 参数是 UICollectionViewUpdateItem 实例的数组,每个元素都移动到新的索引路径。
    open func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem])

    // 在更新后的动画块内调用此方法
    open func finalizeCollectionViewUpdates() 

    // 当集合视图的边界在动画块内改变时,集合视图会调用此方法,以在新边界内显示单元格之前
    open func prepare(forAnimatedBoundsChange oldBounds: CGRect) 

    // 也在动画块内调用此方法
    open func finalizeAnimatedBoundsChange() 

    // 当集合视图在即将到来和即将离开的布局上进行布局转换动画之前,会调用此方法
    @available(iOS 7.0, *)
    open func prepareForTransition(to newLayout: UICollectionViewLayout)

    @available(iOS 7.0, *)
    open func prepareForTransition(from oldLayout: UICollectionViewLayout)

    // 在转换后的动画块内调用此方法
    @available(iOS 7.0, *)
    open func finalizeLayoutTransition() 

    // 当集合视图进行如批量更新块或动画边界更改的动画转换时,调用这组方法。
    // 对于无效化之前的屏幕上的每个元素,将调用 finalLayoutAttributesForDisappearingXXX 并从屏幕上的内容设置动画到最终的属性。
    // 对于无效化之后屏幕上的每个元素,将调用 initialLayoutAttributesForAppearingXXX 并设置从这些初始属性到最终在屏幕上的内容的动画。
    open func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

    open func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

    open func initialLayoutAttributesForAppearingSupplementaryElement(ofKind elementKind: String, at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

    open func finalLayoutAttributesForDisappearingSupplementaryElement(ofKind elementKind: String, at elementIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

    open func initialLayoutAttributesForAppearingDecorationElement(ofKind elementKind: String, at decorationIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

    open func finalLayoutAttributesForDisappearingDecorationElement(ofKind elementKind: String, at decorationIndexPath: IndexPath) -> UICollectionViewLayoutAttributes?

    // 这些方法由集合视图在更新块期间调用。
    // 返回一个索引路径数组,以指示布局响应更新而正在删除或插入的视图。
    @available(iOS 7.0, *)
    open func indexPathsToDeleteForSupplementaryView(ofKind elementKind: String) -> [IndexPath]

    @available(iOS 7.0, *)
    open func indexPathsToDeleteForDecorationView(ofKind elementKind: String) -> [IndexPath]

    @available(iOS 7.0, *)
    open func indexPathsToInsertForSupplementaryView(ofKind elementKind: String) -> [IndexPath]

    @available(iOS 7.0, *)
    open func indexPathsToInsertForDecorationView(ofKind elementKind: String) -> [IndexPath]
}

extension UICollectionViewLayout {
    // 在用户互动移动项目时,这个方法返回移动项目的目标索引路径
    @available(iOS 9.0, *)
    open func targetIndexPath(forInteractivelyMovingItem previousIndexPath: IndexPath, withPosition position: CGPoint) -> IndexPath

    // 在用户互动移动项目时,这个方法返回移动项目的布局属性
    @available(iOS 9.0, *)
    open func layoutAttributesForInteractivelyMovingItem(at indexPath: IndexPath, withTargetPosition position: CGPoint) -> UICollectionViewLayoutAttributes

    // 在用户互动移动项目时,这个方法返回无效化的上下文,用于更新布局
    @available(iOS 9.0, *)
    open func invalidationContext(forInteractivelyMovingItems targetIndexPaths: [IndexPath], withTargetPosition targetPosition: CGPoint, previousIndexPaths: [IndexPath], previousPosition: CGPoint) -> UICollectionViewLayoutInvalidationContext

    // 在用户完成互动移动项目时,这个方法返回无效化的上下文,用于更新布局
    @available(iOS 9.0, *)
    open func invalidationContextForEndingInteractiveMovementOfItems(toFinalIndexPaths indexPaths: [IndexPath], previousIndexPaths: [IndexPath], movementCancelled: Bool) -> UICollectionViewLayoutInvalidationContext
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容