On the realization of waterfall flow There are many kinds of solutions on the Web, custom controls, Tableview+scrollview,uicollectionview is used to display the collection view after IOS6 is released, it has been published for about three years, but the knowledge point is unchanged, The collection view provides a more elegant way to display pictures or text messages. Uicollectionview and UITableView are similar, Uicollectionviewcontroller and Uitableviewcontroller are responsible for the view, store the required data, and can handle the data source and the Trust agreement.
Simple Waterfall Flow
First of all, a simple uicollectionview implementation of the waterfall flow process, familiar with the implementation of a slightly practical waterfall flow:
1. Drag and drop a uicollectionview in the storyboard in the view with the following effect:
2. Create a subclass Mycollectionviewcell that inherits the child Uicollectionviewcell , and select the cell class as Mycollectionviewcell. and set identifier to MyCell.
3. Implement uicollectionviewdelegate,uicollectionviewdatasource , set the range, set the number of items in the range, and create reusable cells:
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{ return 1;} -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section{ return 100;} -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{ Mycollectionviewcell *mycell=[collectionview dequeuereusablecellwithreuseidentifier: @ "MyCell [Forindexpath:indexpath]; [MyCell Setbackgroundcolor:[uicolor Greencolor]; return MyCell;}
Set up data sources and proxies in Viewdidload:
self.collectionview.delegate=self; self.collectionview.datasource=self; Self.collectionview.backgroundcolor=[uicolor Whitecolor];
The final effect is as follows:
Masonry layout (waterfall Flow)
The above effect is still relatively simple, in many cases all the width of the cell is certain, but the height is not sure, this is some people say the fixed width is high, mainly from the visual aesthetic perspective, of course, we can achieve Uicollectionviewdelegateflowlayout to change the cell size:
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{ cgfloat height=100+ (Arc4random ()%100); Return Cgsizemake (at height);}
Through the effect we can find that the center of the cell in the same row is located in the same y-axis coordinates:
Masonry layouts (masonry layout) is the earliest use of Pinterest, masonry simple understanding is the front of the brick when each brick ken can be rectangular or square, the final effect is rectangular or square, the domestic words are basically called waterfall flow, the previous web site is popular , now on the mobile phone, some web and even the entire content of the site is in a waterfall stream page, but also a bit of aesthetic fatigue feeling.
This time first look at the effect of the final implementation:
This should be considered the most common layout pattern, this implementation is very simple, for the demo effect did not take any optimization measures, only for the entry reference, set to store all the height of the array:
Stores all the heights of the array @property (strong,nonatomic) Nsmutablearray *heightarr;
Add the height to the array:
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{ cgfloat height=100+ (Arc4random ()%160); [Self.heightarr addobject:[nsstring stringwithformat:@ "%f", height]]; Return Cgsizemake (at height);}
To modify the position of each row of cells:
[MyCell Setbackgroundcolor:[uicolor greencolor]];//return mycell;//}-(Uicollectionviewcell *) CollectionView: (UI CollectionView *) CollectionView Cellforitematindexpath: (Nsindexpath *) indexpath{Mycollectionviewcell *myCell=[ CollectionView dequeuereusablecellwithreuseidentifier: @ "MyCell" Forindexpath:indexpath]; [MyCell Setbackgroundcolor:[uicolor Redcolor]; Nsinteger remainder=indexpath.row%3; Nsinteger CURRENTROW=INDEXPATH.ROW/3; CGFloat Currentheight=[self.heightarr[indexpath.row] Floatvalue]; CGFloat positonx=100*remainder+10* (remainder+1); CGFloat positiony= (currentrow+1) *10; for (Nsinteger i=0; i<currentrow; i++) {Nsinteger position=remainder+i*3; Positiony+=[self.heightarr[position] Floatvalue]; } mycell.frame = CGRectMake (Positonx, positiony,100,currentheight); Nsuinteger *randomnumber=arc4random_uniform (9); NSString *girlfilename = [NSString stringwithformat:@ "girl%lu.jpg", (unsigned LONG) Randomnumber]; Uiimageview *imageview = [[Uiimageview alloc] initwithimage:[uiimage Imagenamed:girlfilename]]; [MyCell Setbackgroundview:imageview]; return MyCell;}
Effect Demo:
iOS development-uicollectionview for waterfall streaming