美文网首页
UICollectionViewLayout Apple 官方

UICollectionViewLayout Apple 官方

作者: DingGa | 来源:发表于2023-06-03 20:33 被阅读0次

    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
    }
    
    

    相关文章

      网友评论

          本文标题:UICollectionViewLayout Apple 官方

          本文链接:https://www.haomeiwen.com/subject/dcmvedtx.html