官文链接:UICollectionViewLayout - UIKit
The UICollectionViewLayout class is an abstract base class that you subclass and use to generate layout information for a collection view. The job of a layout object is to determine the placement of cells, supplementary views, and decoration views inside the collection view’s bounds and to report that information to the collection view when asked. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.
UICollectionViewLayout是一个用于子类化的抽象类。他的作用是规范背景图,区头,条目的位置。并将布局信息返回给UICollectionView。UICollectionView使用这些信息进行布局。
Overview
You must subclass UICollectionViewLayout in order to use it. Before you consider subclassing, though, you should look at the UICollectionViewFlowLayout class to see if it can be adapted to your layout needs.
你必须子类化该类,在此之前,你需要知道它是否能满足你的需求。
Subclassing Notes
The main job of a layout object is to provide information about the position and visual state of items in the collection view. The layout object does not create the views for which it provides the layout. Those views are created by the collection view’s data source. Instead, the layout object defines the position and size of visual elements based on the design of the layout.
该类的主要作用是提供条目的位置信息。并不创建条目。条目被dataSource创建。他只是根据设计定义条目的位置和大小。
Collection views have three types of visual elements that need to be laid out:
Cells are the main elements positioned by the layout. Each cell represents a single data item in the collection. A collection view can have a single group of cells or it can divide those cells into multiple sections. The layout object’s main job is to arrange the cells in the collection view’s content area.
Supplementary views present data but are different than cells. Unlike cells, supplementary views cannot be selected by the user. Instead, you use supplementary views to implement things like header and footer views for a given section or for the entire collection view. Supplementary views are optional and their use and placement is defined by the layout object.
Decoration views are visual adornments that cannot be selected and are not inherently tied to the data of the collection view. Decoration views are another type of supplementary view. Like supplementary views, they are optional and their use and placement is defined by the layout object.
他的作用对象主要包括单元格,补充视图(类似区头区尾),装饰视图(类似背景图)
The collection view asks its layout object to provide layout information for these elements at many different times. Every cell and view that appears on screen is positioned using information from the layout object. Similarly, every time items are inserted into or deleted from the collection view, additional layout occurs for the items being added or removed. However, the collection view always limits layout to the objects that are visible onscreen.
当增删条目时,相应的布局也被删除。collectionView经常对可见的条目进行约束。
Every layout object should implement the following methods:(以下方法均应该实现)
collectionViewContentSize
layoutAttributesForElementsInRect:
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath: (if your layout supports supplementary views)
layoutAttributesForDecorationViewOfKind:atIndexPath: (if your layout supports decoration views)
shouldInvalidateLayoutForBoundsChange:
These methods provide the fundamental layout information that the collection view needs to place contents on the screen. Of course, if your layout does not support supplementary or decoration views, do not implement the corresponding methods.
这些方法提供了基本的布局信息。如果不需要补充视图或者装视图,可以不用实现相应的方法。
When the data in the collection view changes and items are to be inserted or deleted, the collection view asks its layout object to update the layout information. Specifically, any item that is moved, added, or deleted must have its layout information updated to reflect its new location. For moved items, the collection view uses the standard methods to retrieve the item’s updated layout attributes. For items being inserted or deleted, the collection view calls some different methods, which you should override to provide the appropriate layout information:
当数据源改变时,collectionView会更新布局信息。条目的移动,增删必须要将它的更新它的位置信息。对于移动条目,collectionView必须用标准的方法检索它的布局信息。对于插入和删除,collectionView调用其它的方法,你应该重写这些方法来提供恰当的布局信息。
initialLayoutAttributesForAppearingItemAtIndexPath:
initialLayoutAttributesForAppearingSupplementaryElementOfKind:atIndexPath:
initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath:
finalLayoutAttributesForDisappearingItemAtIndexPath:
finalLayoutAttributesForDisappearingSupplementaryElementOfKind:atIndexPath:
finalLayoutAttributesForDisappearingDecorationElementOfKind:atIndexPath:
In addition to these methods, you can also override the prepareForCollectionViewUpdates: to handle any layout-related preparation. You can also override the finalizeCollectionViewUpdates method and use it to add animations to the overall animation block or to implement any final layout-related tasks.
除了这些方法,你可以重写prepareForCollectionViewUpdates:方法处理布局准备工作。finalizeCollectionViewUpdates方法可以添加动画或者实现布局相关的任务。
Optimizing Layout Performance Using Invalidation Contexts(优化布局性能)
When designing your custom layouts, you can improve performance by invalidating only those parts of your layout that actually changed. When you change items, calling the invalidateLayout method forces the collection view to recompute all of its layout information and reapply it. A better solution is to recompute only the layout information that changed, which is exactly what invalidation contexts allow you to do. An invalidation context lets you specify which parts of the layout changed. The layout object can then use that information to minimize the amount of data it recomputes.
优化思想:当布局信息改变时,只对那些实际改变的布局对象进行修改。
To define a custom invalidation context for your layout, subclass the UICollectionViewLayoutInvalidationContext class. In your subclass, define custom properties that represent the parts of your layout data that can be recomputed independently. When you need to invalidate your layout at runtime, create an instance of your invalidation context subclass, configure the custom properties based on what layout information changed, and pass that object to your layout’s invalidateLayoutWithContext: method. Your custom implementation of that method can use the information in the invalidation context to recompute only the portions of your layout that changed.
子类化UICollectionViewLayoutInvalidationContext获取一个自定义的invalidation context。定义一个属性代表需要进行重新计算的数据源。在运行时,你需要使你的布局失效是,创建一个该类的实例,基于将要改变的布局类配置它的数据属性。并且将对象传递给invalidateLayoutWithContext:方法。对这个方法的声明可以使用实例内的数据计算部分需要改变的布局类。
If you define a custom invalidation context class for your layout object, you should also override the invalidationContextClass method and return your custom class. The collection view always creates an instance of the class you specify when it needs an invalidation context. Returning your custom subclass from this method ensures that your layout object always has the invalidation context it expects.
如果你定义了一个invalidation context,要重写invalidationContextClass方法,并且返回你的这个类。当它需要这对象时,collectionView总是会创建一个这样的对象。