Collection View Basics

Collection View Basics

To present its content onscreen, a collection view cooperates with many different objects. Some objects are custom and must be provided by your app. For example, your app must provide a data source object that tells the collection view how many items there are to display. Other objects are provided by UIKit and are part of the basic collection view design.

Like tables, collection views are data-oriented objects whose implementation involves a collaboration with your app’s objects. Understanding what you have to do in your code requires a little background information about how a collection view does what it does.

为了在屏幕上展示其内容,collection view需要和很多不同的对象协作。有些对象是自定义的,并且必须由你的APP提供。例如,你的APP必须提供一个data source(数据源对象),用来告诉collection view有多少items需要被显示。其他对象是有UIKit提供,这是设计collection view时的一个基本考虑(部分)

A Collection View Is a Collaboration of Objects

The design of collection views separates the data being presented from the way that data is arranged and presented onscreen. Although your app is strictly responsible for managing the data to be presented, its visual presentation is managed by many different objects. Table 1-1 lists the collection view classes in UIKit and organizes them by the roles they play in implementing a collection view interface. Most of the classes are designed to be used as is without any need for subclassing, so you can usually implement a collection view with very little code. And when you want to go beyond the provided behavior, you can subclass and provide that behavior

collection views在设计时就考虑分离数据布局。尽管你的APP是严格的负责数据,但是它的视觉呈现是被不同对象管理的。表1-1列出了UIKit中的collection view的相关类,并按照它们扮演的角色来组织(呈现)它们。大多数类都被设计成不需要子类化的,所有一般来说你可以使用非常少的代码实现一个collection view。而当你想要实现更多功能,你可以子类化并且提供相应的功能

Table 1-1 The classes and protocols for implementing collection views

Purpose Classes/Protocols Description
Top-level containment and management UICollectionView
UICollectionViewController
* UICollectionView对象定义了集合视图内容的可见区域. 这个类事UIScrollView的子类,可以包含一个大的可滚动区域.这个类可以简化数据呈现(基于这个collectionView的layout).
* UICollectionViewController对象为集合视图提供一个控制器级别的管理支持, Its use is optional(可选的).
Content management UICollectionViewDataSource protocol
UICollectionViewDelegate protocol
* data source是collectionView中最重要的对象了,==你必须提供它==. data source管理collection view的内容,并且创建需要展示的views. 实现数据源对象,你必须要创建一个遵守UICollectionViewDataSource协议的对象.
* delegate对象让你可以拦截集合视图的交互消息,并且允许自定义视图的行为. 例如,你使用delegate对象来跟踪items(cell)的选中和高亮. 不想数据源对象,代理对象是optional(可选的).
* 具体的实现data sourcedelegate可以查看下一个章节 《Designing Your Data Source and Delegate》.
Presentation UICollectionReusableView
UICollectionViewCell
* 在集合视图中呈现的所有views必须是UICollectionReusableView类的实例 . 这个类提供循环(回收)机制。. Recycling views (instead of creating new ones)提高用户体验,并且==尤其在滚动的时候==.
* A UICollectionViewCell是一个可重用的具体(明确)类型 .
Layout UICollectionViewLayout
UICollectionViewLayoutAttributes
UICollectionViewUpdateItem
* UICollectionViewLayout 被称为(are referred to as) layout objects的子类,在集合视图中负责定义cells and reusable viewslocation, size, and visual attributes .
* 在布局过程中, 一个layout对象创建一个layout attribute对象 (UICollectionViewLayoutAttributes类的实例),这个对象用来告诉集合视图在哪里并且如何(where and how)展示cells 和 reusable views.
* layout对象接收UICollectionViewUpdateItem类的实例,不论是data items的插入,删除,或者移动. 你不需要自己创建这个类的实例.
* layout对象的具体信息, 查看章节 《The Layout Object Controls the Visual Presentation》.
Flow layout UICollectionViewFlowLayout
UICollectionViewDelegateFlowLayout protocol
UICollectionViewFlowLayout是你实现grids 或者其他线性布局(line-based layouts)的具体布局对象. 你可以按原样使用该类,也可以与流代理对象结合使用,从而允许你动态地自定义布局信息.

Figure 1-1 (图1-1)显示了与集合视图关联的核心对象之间的关系.集合视图从数据源获取要显示的cells的信息. data source and delegate对象是你app提供的自定义对象,用来管理内容,包括cells的选中和高亮. layout对象决定cell所属的位置,并以一个或多个layout attribute对象的格式下来传递信息给集合视图. collection view合并layout和cells来创建最终的可视化呈现.

Collection View Basics_第1张图片
cv_objects_2x.png

When creating a collection view interface, you first add a UICollectionView object to your storyboard or nib file. Think of the collection view as the central hub, from which all other objects emanate. After adding that object, you can begin to configure any related objects, such as the data source or delegate. All configurations are centered around the collection view itself. For example, you never create a layout object without also creating a collection view object.

创建集合视图界面时,首先将UICollectionView对象添加到storyboard or nib文件。将集合视图看作是所有其他物体发出的中心枢纽。添加collection view后,你可以开始配置任何其他关联的对象,例如data source or delegate。所有配置都围绕在collection view本身。例如,你从不创建layout对象,也不创建集合视图对象本身

Reusable Views Improve Performance

Collection views employ a view recycling program to improve efficiency. As views move offscreen, they are removed from view and placed in a reuse queue instead of being deleted. As new content is scrolled onscreen, views are removed from the queue and repurposed with new content. To facilitate this recycling and reuse, all views displayed by the collection view must descend from the UICollectionReusableView class.

Collection views support three distinct types of reusable views, each of which has a specific intended usage:

  • Cells present the main content of your collection view. The job of a cell is to present the content for a single item from your data source object. Each cell must be an instance of the UICollectionViewCell class, which you may subclass as needed to present your content. Cell objects provide inherent support for managing their own selection and highlight state. To actually apply a highlight to a cell, you must write some custom code. For information on implementing cell highlighting/selecting, see Managing the Visual State for Selections and Highlights.
  • Supplementary views display information about a section. Like cells, supplementary views are data driven. Unlike cells, supplementary views are not mandatory, and their usage and placement is controlled by the layout object being used. For example, the flow layout supports headers and footers as optional supplementary views.
  • Decoration views are visual adornments that are wholly owned by the layout object and are not tied to any data in your data source object. For example, a layout object might use decoration views to implement a custom background appearance.

Unlike table views, collection views impose no specific style on the cells and supplementary views provided by your data source. Instead, the basic reusable view classes are blank canvases for you to modify. For example, you can use them to build small view hierarchies, to display images, or even to draw content dynamically.

Your data source object is responsible for providing the cells and supplementary views used by its associated collection view. However, the data source never creates views directly. When asked for a view, your data source dequeues a view of the desired type using the methods of the collection view. The dequeueing process always returns a valid view, either by retrieving one from a reuse queue or by using a class, nib file, or storyboard you provide to create a new view.

For information about how to create and configure views from your data source, see Configuring Cells and Supplementary Views.

集合视图采用(使用)view回收来提高效率。当views移出屏幕时,它们将从view中移除,并置于重用队列中,而不是被删除。随着新内容在屏幕上滚动,views将从队列中移除,并用新内容重新使用(repurposed)。为了便于回收和再利用,集合视图显示的所有视图都必须继承从UICollectionReusableView类。

集合视图支持三种不同类型的可重用视图,每种视图都有特定的预期用法:

  • cells显示集合视图的主要内容。cell的作用是从数据源(data source)对象中呈现单个item的内容。每个cell都必须是UICollectionViewCell的实例,你可以根据需要进行子类化来显示你自己的内容。cells对象为管理自己的选中和高亮状态提供了固有的支持。要实现cell的高亮,你需要写一些自定义代码。具体实现 cell的高亮和选中,查看章节《Managing the Visual State for Selections and Highlights》
  • Supplementary views(补充视图)显示section信息. 和cells一样的是, supplementary views是数据(data)驱动. 和cell不一样的是, supplementary views 不是必须的, 并且他们的使用和排布由正在使用的layout对象控制. 例如, flow layout 支持 headers and footers 作为可选的 supplementary views.
  • Decoration views(装饰视图)是由布局对象完全拥有的可视装饰,并且不受任何数据源对象中的任何数据束缚. 例如,布局对象可能使用装饰视图来实现自定义背景外观(比如书城的项目,在cell外层有装饰).

与table views不同,集​​合视图不会在数据源提供的cellssupplementary view上施加特定的样式。相反,基本的reusable view 是空白的画布供你来定制。例如,你可以使用它们来创建小的视图层次结构,用来展示图片,甚至动态的绘制内容。

data source对象给关联的collectionView提供cellssupplementary view。但是,data source从来没有直接创建views。当需要创建一个view时,data source使用集合视图提供的方法查询(dequeues)所需类型的view。这个查询过程,总是返回一个有效的view,要么从重用队列中检索出一个,要么通过你提供的class,nib,storyboard中创建一个新的

The Layout Object Controls the Visual Presentation

The layout object is solely responsible for determining the placement and visual styling of items within the collection view. Although your data source object provides the views and the actual content, the layout object determines the size, location, and other appearance-related attributes of those views. This separation of responsibilities makes it possible to change layouts dynamically without changing any of the data objects managed by your app.

The layout process used by collection views is related to, but distinct from, the layout process used by the rest of your app’s views. In other words, do not confuse what a layout object does with the layoutSubviews method used to reposition child views inside a parent view. A layout object never touches the views it manages directly because it does not actually own any of those views. Instead, it generates attributes that describe the location, size, and visual appearance of the cells, supplementary views, and decoration views in the collection view. It is then the job of the collection view to apply those attributes to the actual view objects.

There are no limits to how a layout object can affect the views in a collection view. A layout object can move some views but not others. It can move views only a little bit, or it can move them randomly around the screen. It can even reposition views without any regard for the surrounding views. For example, a layout object can stack views on top of each other if it wants. The only real limitation is how the layout object affects the visual style you want your app to have.

layout对象全权负责确定集合视图中items的布局和视觉样式。尽管data source提供了views和实际内容,但是layout对象决定了这些视图的大小,位置以及其他与外观相关的属性。这种责任分离使得动态改变布局成为可能,而不需要改变由你的应用管理的任何数据对象。

集合视图使用的布局过程与应用程序其余视图使用的布局过程相关,但与之不同。换句话说,不要混淆layout对象做的事情与layoutSubviews用于在父视图内重新定位子视图的方法。布局对象从不触及其直接管理的views,因为它实际上并不拥有任何这些views。而是生成描述集合视图中cells, supplementary views, and decoration views的位置,大小和可视外观的属性。将这些属性应用到实际的视图对象上,这才是集合视图的工作。

集合视图中对于怎么通过layout对象来布局是没有限制的,可以任意的布局。layout对象可以移动某些视图,另外得不移动。 它可以移动一点点位移,或者可以在屏幕上随意移动它们。它甚至可以重新定位视图,而不考虑周围的视图。例如,如果需要,layout对象可以将视图堆叠在一起。唯一真正的限制是布局对象如何符合你app的需求

Figure 1-2 shows how a vertically scrolling flow layout arranges its cells and supplementary views. In a vertically scrolling flow layout, the width of the content area remains fixed and the height grows to accommodate the content. To compute the area, the layout object places views and cells one at a time, choosing the most appropriate location for each. In the case of the flow layout, the size of the cells and supplementary views are specified as properties, either on the layout object or by using a delegate. Computing the layout is just a matter of using those properties to place each view.

图1-2显示了垂直滚动流水布局如何排列cells and supplementary views。在垂直滚动流水布局中,内容区域的宽度保持固定,并且高度增长以适应内容。为了计算面积,layout对象一次放置所有的views和cells,为每个布局对象选择最合适的位置。在流水布局的情况下,cells and supplementary views的大小被指定为要么是layout对象要么使用delegate的属性。计算布局只是使用这些属性来放置每个视图的问题。

Figure 1-2 The layout object provides layout metrics

Collection View Basics_第2张图片
cv_layout_basics_2x-2.png

Layout objects control more than just the size and position of their views. The layout object can specify other view-related attributes, such as its transparency, its transform in 3D space, and its visibility (if any) above or below other views. These attributes let you create more interesting layouts. For example, you might create stacks of cells by placing the views on top of one another and changing their z-ordering, or you might use a transform to rotate them on any axis.

For detailed information about how a layout object fulfills its responsibilities to the collection view, see Creating Custom Layouts.

布局对象不仅控制其视图的大小和位置。布局对象可以指定其他视图相关的属性,例如其透明度,3D空间中的变换以及在其他视图之上或之下的可见性(如果有的话)。这些属性让你创建更有趣的布局。例如,您可以通过将视图置于另一个视图之上并更改它们的z轴来创建cell堆栈,也可以使用transform(变换)在任何轴上旋转它们。

Collection Views Initiate Animations Automatically

Collection views build in support for animations at a fundamental level. When you insert (or delete) items or sections, the collection view automatically animates any views impacted by the change. For example, when you insert an item, items after the insertion point are usually shifted to make room for the new item. The collection view can create these animations because it detects the current position of items and can calculate their final positions after the insertion takes place. Thus, it can animate each item from its initial position to its final position.

In addition to animating insertions, deletions, and move operations, you can invalidate the layout at any time and force it to recalculate its layout attributes. Invalidating the layout does not animate items directly; when you invalidate the layout, the collection view displays the items in their newly calculated positions without animating them. Instead in a custom layout, you might use this behavior to position cells at regular intervals and create an animated effect.

集合视图支持基础级别的动画。插入(或删除)items或section时,集合视图会自动为受该更改影响的所有视图生成动画。例如,当您插入一个item时,插入点之后的items通常会被移动以为新item腾出空间。集合视图可以创建这些动画,因为它可以检测items的当前位置,并可以在插入后计算它们的最终位置。因此,它可以将每个项目从其初始位置移动到其最终位置。

除了动画插入,删除和移动操作之外,您可以随时使布局无效,并强制重新计算其布局属性。使layout无效不直接动画item; 当您使layout无效时,集合视图会在新计算的位置显示这些项目,而不会动画它们。相反,在自定义布局中,您可以使用此行为来定位cells并创建动画效果

你可能感兴趣的:(Collection View Basics)