1112 - iOS之 UICollectionViewLayout 类

Class

UICollectionViewLayout

--An abstract base class for generating layout information for a collection view.

    一个抽象基础类,用于给collection view提供布局信息

 

Declaration

class UICollectionViewLayout : NSObject -- 直接继承于NSObject

Overview

--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.

     layout对象的工作是确定cell、附加视图、装饰视图的位置,并在必要时把位置等布局信息报告给collection view。然后colllection view就可根据这些信息呈现视图了。

--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.

   如果你要使用布局对象,你必须子类化UICollectionViewLayout来使用。

 

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.

    layout实例的工作时提供items的位置信息和可视化信息,但不负责创建items的view。items的view是由data source创建的。

 

--Collection views have three types of visual elements that need to be laid out:

    collection view 有三种可视化元素是需要被布局的:

  • 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.

         第一个可视元素,Cell。cell是layout对象最主要去布局的元素。layout对象主要是在collection view 的区域内排列cells

  • 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.

     每次插入或者增加items,都会增加额外的布局信息,但不管如何,collection view的全部布局都限制在可见的屏幕内。

 

Methods to Override -- 提供给你复写的方法与属性,可结合super.method使用

 

--Every layout object should implement the following methods:

    每一个layout对象都应该实现以下的方法,你复写这些方法的时候,第一行用super,应该是这个意思。

                                                                                                                  --方法: 返回某种额外视图的布局特征

                                                                                                                 --方法:返回某种装饰视图的布局特征

                                                                                                                 --方法:返回“当bounds改变时”是否需要更新布局

 

--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.

   当collection view的data或者items发生改变时,collection view都会请问layout对象是否更新布局

--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:

    插入和删除items时,collection view会自动调用以下方法,所以你可以在子类里复写这些方法:

                                                                                                                      --方法:初始化将要插入的额外视图的布局特征

                                                                                                                    --方法:初始化将要插入的装饰视图的布局特征

                                                                                                                --方法:设置要删除的某item的最后布局特征

                                                                                                              --方法:设置要删除的额外视图的最后布局特征

                                                                                                                --方法:设置要删除的装饰视图的最后布局特征

--In addition to these methods, you can also override the prepare(forCollectionViewUpdates:) 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.

     除了上述方法,你还可以复写prepare(forCollectionViewUpdates:)finalizeCollectionViewUpdates()方法来设置前期布局工作和最后的布局工作,最后布局工作可以是添加动画block,即动画结束后才执行的block。

 

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.

     你可以无效化那些真正发生改变的layout,这样collection就会去更新这些无效化了的layout,而不是更新全部layout。当你改变items时,你可以调用invalidateLayout() 方法来强迫collection view重新计算所有的布局信息并且更新。

--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.

     “无效化上下文”允许你指定哪一部分的layout发生改变。这样可以最小化重算布局信息的计算量。   

--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.

     你子类化一个UICollectionViewLayoutInvalidationContext类,在子类中自定义与layout相关的属性,那么这些layout信息会立马被重新计算。

--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 invalidateLayout(with:) 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.

           如果要在运行期更新layout信息,那么你使用子类化UICollectionViewLayoutInvalidationContext的实例,把该实例闯入到layout的invalidateLayout(with:)方法中,就可以在运行期更新布局信息了。

--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.

       子类化UICollectionViewLayoutInvalidationContext的实例,是在layout的invalidationContextClass 方法中与layout绑定的,将子类作为方法的返回值即可,会自动创建实例的。

 

Topics -- 专题

Initializing the Collection View -- 初始化collection view 的layout

init()

--Initializes the collection view layout object.

init?(coder: NSCoder)

 

Getting the Collection View Information -- 获取collection view的信息

 

var collectionView: UICollectionView?                             --属性:返回使用这个layout的collection view

--The collection view object currently using this layout object.

 

var collectionViewContentSize: CGSize                      --属性:返回collection view 的contentsize

--Returns the width and height of the collection view’s contents.

 

Providing Layout Attributes -- 提供布局特征

 

class var layoutAttributesClass: AnyClass                       --静态属性:返回你使用的布局特征对象

--Returns the class to use when creating layout attributes objects.

 

func prepare()                                                                                  --方法:每次更新布局时首先自动调用此方法

--Tells the layout object to update the current layout.

 

func layoutAttributesForElements(in: CGRect) -> [UICollectionViewLayoutAttributes]?

                                                                                                               --方法:返回指定矩形内所有cells的布局特征,以数组形式返回

--Returns the layout attributes for all of the cells and views in the specified rectangle.

 

func layoutAttributesForItem(at: IndexPath) -> UICollectionViewLayoutAttributes?

                                                                                                           --方法:返回某索引item的布局特征

--Returns the layout attributes for the item at the specified index path.

 

func layoutAttributesForInteractivelyMovingItem(at: IndexPath, withTargetPosition: CGPoint) -> UICollectionViewLayoutAttributes

                                                                                                               --方法:返回客户正在移动的item的布局特征

--Returns the layout attributes of an item when it is being moved interactively by the user.

 

func layoutAttributesForSupplementaryView(ofKind: String, at: IndexPath) -> UICollectionViewLayoutAttributes?

                                                                                                          --方法:返回某额外视图的布局特征

--Returns the layout attributes for the specified supplementary view.

 

func layoutAttributesForDecorationView(ofKind: String, at: IndexPath) -> UICollectionViewLayoutAttributes?

                                                                                                          --方法:返回某装饰视图的布局特征

--Returns the layout attributes for the specified decoration view.

 

func targetContentOffset(forProposedContentOffset: CGPoint) -> CGPoint

                                                                                           --方法:返回布局更新或者动画结束后的collection view的content offset

--Returns the content offset to use after an animated layout update or change.

 

func targetContentOffset(forProposedContentOffset: CGPoint, withScrollingVelocity: CGPoint) -> CGPoint

                                                                                         --方法:返回停止滑动时 的那一点

--Returns the point at which to stop scrolling.

 

Responding to Collection View Updates -- 响应collection view的更新

 

func prepare(forCollectionViewUpdates: [UICollectionViewUpdateItem])

                                                                    --方法:通知layout对象,collection view的内容即将改变,参数是将要发生改变的items数组

--Notifies the layout object that the contents of the collection view are about to change.

 

func finalizeCollectionViewUpdates()

                                                                                      --方法:layout更新的最后一步自动调用此方法

--Performs any additional animations or clean up needed during a collection view update.

 

func indexPathsToInsertForSupplementaryView(ofKind: String) -> [IndexPath]

                                                                                      --方法:返回某类型额外视图的索引数组,这里可以修改额外视图布局

--Returns an array of index paths for the supplementary views you want to add to the layout.

 

func indexPathsToInsertForDecorationView(ofKind: String) -> [IndexPath]

                                                                                      --方法:返回某类型装饰视图的索引数组,这里可以修改装饰视图的布局

--Returns an array of index paths representing the decoration views to add.

 

func initialLayoutAttributesForAppearingItem(at: IndexPath) -> UICollectionViewLayoutAttributes?

                                                                                      --方法:返回即将插入的items的布局特征,可以在这里初始化items布局

--Returns the starting layout information for an item being inserted into the collection view.

 

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

                                                                                      --方法:返回即将插入的额外视图的布局特征,可以在这里初始化额外视图的布局

--Returns the starting layout information for a supplementary view being inserted into the collection view.

 

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

                                                                                      --方法:返回即将插入的装饰视图的布局特征,可以在这里初始化装饰视图的布局

--Returns the starting layout information for a decoration view being inserted into the collection view.

 

func indexPathsToDeleteForSupplementaryView(ofKind: String) -> [IndexPath]

                                                                                      --方法:返回将要删除的额外视图的索引数组,这里可以做最后的删除操作。

--Returns an array of index paths representing the supplementary views to remove.

 

func indexPathsToDeleteForDecorationView(ofKind: String) -> [IndexPath]

                                                                                      --方法:返回将要删除的装饰视图的索引数组,这里可以做最后的删除操作。

--Returns an array of index paths representing the decoration views to remove.

 

func finalLayoutAttributesForDisappearingItem(at: IndexPath) -> UICollectionViewLayoutAttributes?

                                                                                      --方法:返回将要删除的items的布局特征数组,这里可以做最后的删除操作。

--Returns the final layout information for an item that is about to be removed from the collection view.

 

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

                                                                                      --方法:返回将要删除的额外视图的布局特征数组,这里可以做最后的删除操作。

--Returns the final layout information for a supplementary view that is about to be removed from the collection view.

 

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

                                                                                      --方法:返回将要删除的装饰视图的索引数组,这里可以做最后的删除操作。

--Returns the final layout information for a decoration view that is about to be removed from the collection view.

 

func targetIndexPath(forInteractivelyMovingItem: IndexPath, withPosition: CGPoint) -> IndexPath

                                                                                      --方法:返回某位置点的item的索引,collection view的本地坐标系

--Returns the index path to for an item when it is at the specified location in the collection view’s bounds.

 

Invalidating the Layout -- 无效化layout,然后layout就会重新计算

 

func invalidateLayout()                                            --方法:无效化所有layout,然后会触发layout的重新计算

--Invalidates the current layout and triggers a layout update.

 

func invalidateLayout(with: UICollectionViewLayoutInvalidationContext)

                                                                                           --方法:根据传进来的“无效上下文”参数,来重新计算相关layout

--Invalidates the current layout using the information in the provided context object.

 

class var invalidationContextClass: AnyClass                       --get 静态属性:返回当前正在使用的“无效上下文”

--Returns the class to use when creating an invalidation context for the layout.

 

func shouldInvalidateLayout(forBoundsChange: CGRect) -> Bool

                                                                                                                       --方法:断定bounds发生改变时是否更新layout

--Asks the layout object if the new bounds require a layout update.

 

func invalidationContext(forBoundsChange: CGRect) -> UICollectionViewLayoutInvalidationContext

                                                                                                                  --方法:在这里设置“无效上下文”

--Returns a context object that defines the portions of the layout that should change when a bounds change occurs.

 

func shouldInvalidateLayout(forPreferredLayoutAttributes: UICollectionViewLayoutAttributes, withOriginalAttributes: UICollectionViewLayoutAttributes) -> Bool

                                                                                                                  --方法:断定cell“自我调整”后是否重新计算布局

--Asks the layout object if changes to a self-sizing cell require a layout update.

 

func invalidationContext(forPreferredLayoutAttributes: UICollectionViewLayoutAttributes, withOriginalAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutInvalidationContext

                                                                                         --方法:在这里设置动态cell的“无效上下文”

--Returns a context object that identifies the portions of the layout that should change in response to dynamic cell changes.

 

func invalidationContext(forInteractivelyMovingItems: [IndexPath], withTargetPosition: CGPoint, previousIndexPaths: [IndexPath], previousPosition: CGPoint) -> UICollectionViewLayoutInvalidationContext

                                                                                           --方法:在这里设置移动item时的“无效上下文”

--Returns a context object that identifies the items that are being interactively moved in the layout.

 

func invalidationContextForEndingInteractiveMovementOfItems(toFinalIndexPaths: [IndexPath], previousIndexPaths: [IndexPath], movementCancelled: Bool) -> UICollectionViewLayoutInvalidationContext

                                                                                        --方法:在这里设置已经移动的item的“无效上下文”

--Returns a context object that identifies the items that were moved

 

Coordinating Animated Changes -- 协调可动画化的改变

 

func prepare(forAnimatedBoundsChange: CGRect)

                                                                                          --方法:在 ”动画(动词)“ item的“改变(名词)” 前,自动调用该方法

--Prepares the layout object for animated changes to the view’s bounds or the insertion or deletion of items.

 

func finalizeAnimatedBoundsChange()

                                                                                     --方法:在 ”动画(动词)“ item的“改变(名词)” 后,自动调用该方法

--Cleans up after any animated changes to the view’s bounds or after the insertion or deletion of items.

 

Transitioning Between Layouts -- 在layout之间过渡

 

func prepareForTransition(from: UICollectionViewLayout)

                                                                          --方法:在过渡到新的layout前,自动调用该方法

--Tells the layout object to prepare to be installed as the layout for the collection view.

 

func prepareForTransition(to: UICollectionViewLayout)

                                                                                --方法:在旧layout删除前,自动调用该方法,参数是新的layout

--Tells the layout object that it is about to be removed as the layout for the collection view.

 

func finalizeLayoutTransition()         --方法:在layout的过渡动画发生前,自动调用该方法                                   

--Tells the layout object to perform any final steps before the transition animations occur.

 

Registering Decoration Views -- 注册装饰品视图

 

func register(AnyClass?, forDecorationViewOfKind: String)      --方法:注册装饰视图的类

--Registers a class for use in creating decoration views for a collection view.

 

func register(UINib?, forDecorationViewOfKind: String)    --方法:注册装饰视图的nib file

--Registers a nib file for use in creating decoration views for a collection view.

 

Supporting Right-To-Left Layouts -- 对“从右到左”的布局的支持

 

var developmentLayoutDirection: UIUserInterfaceLayoutDirection          --get属性:获取语言的左右阅读方向

--The direction of the language you used when designing your custom layout.

    属性也是可以在子类中重写的,所以你可以在子类化的时候,重写该属性

 

var flipsHorizontallyInOppositeLayoutDirection: Bool                       --bool属性:断定水平坐标系是否翻转

--A Boolean value indicating whether the horizontal coordinate system is automatically flipped at appropriate times.

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值