UICollectionViewLayout官文

官文鏈接: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總是會創建一個這樣的對象。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容