UIKit User Interface Catalog Part One (Collection Views)

本文為大地瓜原創,歡迎知識共享,轉載請注明出處。
雖然你不注明出處我也沒什么精力和你計較。
作者微信號:christgreenlaw


2017-10-12日更新:
蘋果官網刪除了本文檔,已經找不到這個文檔了,好在我還有pdf版,現共享給大家。
鏈接:http://pan.baidu.com/s/1i5GKG9v 密碼:9g6z


正如文檔的名字,UIKit中的用戶界面目錄,其實簡單的來講,這篇文檔就是把常見的界面組件列了個目錄,大概介紹,而不深入。


Collection Views

A collection view displays an ordered collection of data items using standard or custom layouts. Similar to a table view, a collection view gets data from your custom data source objects and displays it using a combination of cell, layout, and supplementary views. A collection view can display items in a grid or in a custom layout that you design. Regardless of the layout style you choose, a collection view is best suited to display nonhierarchical, ordered data items.

一個collection view展示了一組有序的集合,其中的數據項目使用標準或自定義布局。與table view相似,一個collection view從自定義的數據源對象獲取數據,用cell、layout以及supplementary view將數據展示出來。一個collection view可以在網格中展示項目,也可以用你設計的自定義布局展示項目。不管你選擇什么樣的布局風格,collection view作為展示無層級、有序數據項目的一種方式,都是最佳的。

collection view的一個例子

Purpose. Collection views allow users to:

  • View a catalog of variably sized items, optionally sorted into multiple sections
    //查看一組可變大小的項目、可選地分成多個section
  • Add to, rearrange, and edit a collection of items
    //添加、重排、編輯一組項目
  • Choose from a frequently changing display of items
    //從一組經常變化展示方式的項目中進行選擇

Implementation.

Configuration. Configure collection views in Interface Builder, in the Collection View section of the Attributes Inspector. A few configurations cannot be made through the Attributes Inspector, so you must make them programmatically. You can set other configurations programmatically, too, if you prefer.
//在IB中 Attributes Inspector的Collection View 部分對collection view進行配置。有些配置不能使用Attributes Inspector進行配置,所以你需要用代碼來配置。如果你愿意的話,其他配置也可以在代碼中做。

Attributes Inspector中collection view的配置面板

Content of Collection Views

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, although some custom code must be written to actually apply a highlight to a cell. A UICollectionViewCell object is a specific type of reusable view that you use for your main data items.

cells展示了collection view中的主要內容。cell 的任務是展示數據源對象的一個項目內容。每個cell都必須是UICollectionViewCell 類的實例,你需要根據需求繼承UICollectionViewCell 類來展示你的內容。cell對象為管理它們自己的選擇及高亮狀態提供了繼承支持,雖然要真正的支持cell 的高亮需要寫一些自定義代碼。UICollectionViewCell對象是你用來展示關鍵數據項目的reusable view 的特例。

To manage the visual presentation of data, a collection view works with many related classes, such as UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionReusableView, UICollectionViewCell, UICollectionViewLayout, and UICollectionViewLayoutAttributes.

要管理數據的可視化表現,collection view需要和很多相關的類一起配合完成,比如說:UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionReusableView, UICollectionViewCell, UICollectionViewLayout, and UICollectionViewLayoutAttributes.

Collection views enforce a strict separation between the data being presented and the visual elements used for presentation. Your app is solely responsible for managing the data via your custom data source objects. (To learn how to create these objects, see Designing Your Data Objects.) Your app also provides the view objects used to present that data. The collection view takes your views and—with the help of a layout object, which specifies placement and other visual attributes—does all the work of displaying them onscreen.

collection view在被展示的數據以及用于展示的可視化元素之間強制性的進行了分割。你的app通過你自定義的數據源完全負責數據的管理。(要了解如何創建這些對象,see Designing Your Data Objects)你的應用也提供view對象來展示這個數據。collection view接受你的view,在layout對象(layout對象指明了擺放方式以及其他的可視化屬性)的幫助下,完全承擔了將這些view展示在屏幕上的工作。

屏幕快照 2017-09-11 16.16.54.png

To display content onscreen in an efficient way, a collection view uses the following reusable view objects:

  • Cell. Represents one data item.
  • Supplementary view. Represents information related to the data items, such as a section header or footer.
  • Decoration view. Represents purely decorative content that’s not part of your data, such as a background image.

想有效地展示內容,collection view使用如下的可重用view 對象:

  • Cell. 代表一個數據item。
  • Supplementary view. 代表與data items相關的信息,比如section header或footter
  • Decoration view. 代表純粹的裝飾性內容,并不是數據的一部分。比如背景圖片。
屏幕快照 2017-09-11 16.28.48.png

Because a collection view works with these and other objects to determine the visual presentation of your data, configuring a collection view in Interface Builder means that you need to configure some objects separately.

由于collection view和這些以及其他的對象協同工作來確定你數據的可視化展現,在IB中配置collection view意味著你需要分開獨立配置某些對象。

  • Items. The number of different types of data for which you define distinct cell objects. If your app works with only one type of data item—regardless of the total number of data items you display—set this value to 1.
  • Accessories. The existence of a header or footer view for each section (this property isn’t available for custom layouts). Select Section Header or Section Footer as appropriate.
  • Items. 你所定義的不同類型數據的數量,用以將cell對象區分開來。如果你的app只有一種data item--不管你總共要展示多少data item--將這個值都設置為1。
  • Accessories. 每個section的header或者footer是否存在。(這個值對于自定義布局來說是不可用的)根據情況選擇Section Header或Section Footer。

In Collection Reusable View Attributes inspector—which governs supplementary views, decoration views, and cells—you can set the Identifier (identifier) field. Enter the ID you use in your code to identify the reusable cell, decoration, or supplementary view object.

在Collection Reusable View Attributes inspector中--其管理了supplementary views, decoration views, and cells--你可以設置Identifier域。鍵入你代碼中用來標志reusable cell, decoration, or supplementary view object 的ID。

屏幕快照 2017-09-11 16.29.04.png

Behavior of Collection Views

There are several behaviors you can support in your collection view. For example, you might want to allow users to:

  • Select one or more items
    //選中一個或多個item
  • Insert, delete, and reorder items or sections
    //插入刪除重排item或section
  • Edit an item
    //編輯item
    By default, a collection view detects when the user taps a specific cell and it updates the cell’s selected or highlighted properties as appropriate. You can write code that configures a collection view to support multiple-item selection or that draws the selected or highlighted states yourself. To learn how to support multiple selection or custom selection states, see Managing the Visual State for Selections and Highlights.

默認情況下,collection view檢測用戶點擊一個特定的cell,并且根據情況更新cell的選中屬性或者是高亮屬性。你也可以通過代碼讓collection view支持多選,或者自己畫出選中/高亮狀態。要了解如何支持多選or自定義選中狀態,參見Managing the Visual State for Selections and Highlights

To support insertion, deletion, or reordering of cells in a collection view, you make changes to your data source and then tell the collection view to redisplay the content. By default, a collection view animates the insertion, deletion, or movement of a single item; if you want to animate these changes for multiple items at once, you use code blocks to batch the update. To learn how animate multiple changes to a collection view, see Inserting, Deleting, and Moving Sections and Items. To let users move an item or items by dragging, you also need to incorporate a custom gesture recognizer. (To learn how to do this, see Manipulating Cells and Views.)

要支持插入、刪除、或cell 的重排序,你需要變更數據源,告訴collection view重新顯示內容。默認,collection view會將單一item 的插入、刪除、移動進行動畫處理,如果你希望一次性將多個item的改變都進行動畫,你需要使用block來批量處理更新。要了解如果對collection view進行多個item的動畫,參見 Inserting, Deleting, and Moving Sections and Items。要讓用戶通過拖動移動一個或多個item,你也需要使用一個自定義的手勢識別器(custom gesture recognizer)。 (To learn how to do this, see Manipulating Cells and Views.)

If you support editing for an item, the collection view automatically displays the Edit menu when it detects a long-press gesture on a specific cell. To learn how to support editing in a collection view, see Showing the Edit Menu for a Cell.

如果你想支持編輯item,collection view自動會在檢測到某個cell的長按手勢時顯示一個Edit菜單。To learn how to support editing in a collection view, see Showing the Edit Menu for a Cell.

When configuring cells and supplementary views in a storyboard, you do so by dragging the item onto your collection view and configuring it there. This creates a relationship between the collection view and the corresponding cell or view. For cells, drag a collection view cell from the object library and drop it onto your collection view. Set the custom class and the collection reusable view identifier of your cell to appropriate values.

在IB中配置cell和supplementary view時,你要將item拖拽到collection view上,并在此進行配置。這就在collection view和相應的cell或者view之間建立了聯系。對于cell來說,從object library上拖拽一個cell并放到collection view上。為cell設置好custom class和collection reusable view identifier的值。

Whether the user is selecting or deselecting a cell, the cell’s selected state is always the last thing to change. Taps in a cell always result in changes to the cell’s highlighted state first. Only after the tap sequence ends and any highlights applied during that sequence are removed, does the selected state of the cell change. When designing your cells, you should make sure that the visual appearance of your highlights and selected state do not conflict in unintended ways.

不管是選中還是取消選中cell,cell 的選中狀態永遠是最后變化的。cell 的點擊永遠會先改變cell 的高亮狀態。只有在點擊序列結束且序列過程中高亮狀態移除后,選中狀態才會改變。設計cell時,應該保證高亮和選中狀態不要發生沖突。

When the user performs a long-tap gesture on a cell, the collection view attempts to display an Edit menu for that cell. The Edit menu can be used to cut, copy, and paste cells in the collection view.

用戶長按cell時,collection view會企圖(attempt)為這個cell顯示一個Edit菜單。Edit菜單可以用來剪切、復制、粘貼cell。

If you’re working with the UICollectionViewFlowLayout class, you can use the Attributes inspector to set the “Scroll Direction” (scrollDirection) field to Horizontal or Vertical. Note that this property isn’t available for custom layouts.

如果你使用UICollectionViewFlowLayout類,你可以用Attributes inspector 將“Scroll Direction” (scrollDirection) 域設置為Horizontal or Vertical。要注意的是,這個屬性對于自定義layout是不可用的。

layout

Appearance of Collection Views

Layout

A collection view relies on a layout object to define the layout of its cells, supplementary views, and decoration views.

collection view依賴一個layout 對象來定義cells, supplementary views, and decoration views的layout。

The Layout field determines the layout of the cells. The default value is Flow, which refers to the layout defined by the UICollectionViewFlowLayout class. If you provide a custom layout class, choose Custom instead.

layout域決定了cell 的布局。默認值是Flow,也就是 UICollectionViewFlowLayout類所定義的layout。如果你提供一個自定義的布局類,選擇Custom。

layout

To learn more about creating a custom layout class, see Collection View Programming Guide for iOS.

Background

To use a custom background for a collection view, you can specify a view that's positioned underneath all of the other content and sized automatically to fill the entire bounds of the collection view. You can set this value using the backgroundView property. Because this background view doesn’t scroll with the collection view’s content, it’s not an appropriate way to display a decorative background such as the appearance of wooden shelves.

要給collection view使用一個自定義背景,你可以指明一個view,這個view在所有其他內容之下,自動填充整個collection view的邊界。你可以使用 backgroundView屬性來設置這個值。由于這個background view不隨collection view的內容滾動,它并不適合于顯示類似于“木書架“這樣的decorative background。

Cell Background

To use a custom background for a single collection view cell, you can specify a view that’s positioned behind the cell’s content view and that fills the cell’s bounds. You can set this value using the backgroundView property.

要給單獨的collection view cell使用一個自定義背景,你可以指明一個view,此view放置在cell 的content view之后,填充了cell 的邊界。You can set this value using the backgroundView property.

You can also specify a custom selected background by providing a view that’s displayed above the cell’s background view—and behind the content view—when the user selects the cell. Set this value using the selectedBackgroundView property.

你也可以指明一個自定義的選中狀態下的背景,只需要提供一個view,此view位于cell 的background view之上,而在其content view之下--當用戶選中cell時。Set this value using the selectedBackgroundView property.

Spacing

In the Collection View Flow Layout Size Inspector, you can set size values (in points) for the layout object to use when laying out cells and supplementary views.

在Collection View Flow Layout Size Inspector中,你可以以point為單位設置layout 對象的大小,在布局cell 和supplementary views時進行使用。

spacing

For spacing between cells you can set the following Min Spacing values:
For Cells. The minimum space to maintain between cells on one line.
//一行上cell之間的最小距離
For Lines. The minimum space to maintain between lines of cells.
//多行cell間的最小距離

Cell Padding
cell padding

To add padding around cells so that space appears above, below, or on either side of the cells in a section, use the “Section Insets” fields in the Collection View Flow Layout Size Inspector. Specifying nonzero inset values reduces the amount of space available for laying out cells, which lets you limit the number of cells that can appear on one row or the number of rows that can appear in one section. The insets you can specify are:

要在cell周圍添加padding,調節section中cell的上下左右邊距,use the “Section Insets” fields in the Collection View Flow Layout Size Inspector。聲明一個非零值將會減少布局cell時的邊距值,可以用來限制一行上cell 的數量,以及section中的行數。你可以設置的inset如下:

  • Top. The space to add between the bottom of the header view and the top of the first line of cells.//header view底部和第一行cell頂部之間的間距
  • Bottom. The space to add between the bottom last line of cells and the top of the footer/最后一行cell的底部和footer頂部的間距
  • Left. The space to add between the left edge of the cells and the left edge of the collection view.//cell的左邊和collection view的左邊的間距
  • Right. The space to add between the right edge of the cells and the right edge of the collection view.//cell的右邊和collection view的右邊的間距

Using Auto Layout with Collection Views

You can create Auto Layout constraints between a collection view and other user interface elements. You can create any type of constraint for a collection view besides a baseline constraint.

collection view和其他用戶界面元素之間可以創建Auto Layout約束。除了baseline約束以外也可以創建任何種類的約束(這句話的意思好像就是什么約束都行?besides并不表示否定啊。。。。)

For general information about using Auto Layout with iOS views, see Using Auto Layout with Views.

Making Collection Views Accessible

The data items in a collection view are accessible by default when they are represented by standard UIKit objects, such as UILabel
and UITextField
.
When a collection view changes its onscreen layout, it posts the UIAccessibilityLayoutChangedNotification
notification.
For general information about making iOS views accessible, see Making Views Accessible.

Internationalizing Collection Views

For more information, see Internationalization and Localization Guide.

Elements Similar to a Collection View

The following elements provide similar functionality to a collection view:
Table View. A scrolling view that displays data items in a single-column list. For more information, see Table Views.

Scroll View. A scrolling view that displays content without support for any specific layout or ordering scheme. For more information, see Scroll Views.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,837評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,196評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,688評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,654評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,456評論 6 406
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,955評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,044評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,195評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,725評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,608評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,802評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,318評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,048評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,422評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,673評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,424評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,762評論 2 372

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,692評論 0 23
  • 文/沫花 一花一世界,一樹一經年。 ——題記 風吹花落花伴樹,花樹永生不棄。 我想念他們了。那些我的朋友們。好像很...
    魚利閱讀 385評論 0 0
  • 十二年苦讀,高考一朝解放。 進入大學繼續求學,對于考生、家長來說,是人生最為關鍵的一個階段。除了關注學校外,大學的...
    Miss張的茶館閱讀 407評論 0 1
  • 省政府10月17日01時發布臺風及暴雨一級預警:“莎莉嘉”將于18日中午前后以強臺風或超強臺風級在瓊海到三亞一帶登...
    甘木閱讀 288評論 0 0