Implementation of the waterfall stream for iOS development

Source: Internet
Author: User

Custom layouts, inherited from Uicollectionviewlayout

#import <UIKit/UIKit.h>

@class waterflowlayout;

@protocol Waterflowlayoutdeleagre;

@interface Waterflowlayout:uicollectionviewlayout

@property (Nonatomic,weak) id<waterflowlayoutdeleagre>delegate;

@end

@protocol Waterflowlayoutdeleagre <NSObject>

@required

-(CGFloat) CollectionView: (__kindof uicollectionview *) CollectionView Heightforitematindexpath: (NSIndexPath *) Indexpath;

@end

-------------------------------------------------------------------------

#import "WaterFlowLayout.h"

static CGFloat Const COLOUMMARGINDEFAULT = 10; Gap

static Nsinteger Const COLOUMDEFAULT = 3; You can change the number of columns here

static CGFloat Const ROWMAGINDEGAULT = 10; The gap between lines

@interface Waterflowlayout ()

@property (Nonatomic,strong) nsmutablearray * Attris;

@property (Nonatomic,strong) nsmutablearray * cloumsheight;

@end

@implementation Waterflowlayout

Record the maximum Y value of a cell that is not listed

-(Nsmutablearray *) cloumsheight

{

if (_cloumsheight = = nil) {

_cloumsheight = [Nsmutablearray array];

}

return _cloumsheight;

}

Store property values for each cell

-(Nsmutablearray *) Attris

{

if (_attris = = nil) {

_attris = [Nsmutablearray array];

}

return _attris;

}

Prepare to start layout

-(void) preparelayout

{

[Super Preparelayout];

Clear all column heights before layout starts

[Self.cloumsheight removeallobjects];

Add an initial value for the column height

for (int i=0; i<coloumdefault; i++) {

[Self.cloumsheight addobject:@0];

}

Get the number of cells in the corresponding group

Nsinteger count = [Self.collectionview numberofitemsinsection:0];

Nsmutablearray * Atts = [Nsmutablearray array];

Create a layout property for a cell

for (int i=0; i<count; i++) {

Gets the cell property of the corresponding Indexpath in CollectionView

uicollectionviewlayoutattributes * attributes = [self Layoutattributesforitematindexpath:[nsindexpath indexPathForIt Em:i insection:0]];

[Atts Addobject:attributes];

}

Self.attris = Atts;

}

-(Nullable nsarray<__kindof uicollectionviewlayoutattributes *> *) Layoutattributesforelementsinrect: (CGRect) rect{

Returns the layout properties of all cells

return self.attris;

}

-(uicollectionviewlayoutattributes*) Layoutattributesforitematindexpath: (Nsindexpath *) IndexPath

{

Initialize Layout properties

uicollectionviewlayoutattributes * attributes = [uicollectionviewlayoutattributes Layoutattributesforcellwithindexpath:indexpath];

Let discipline the first data of the array in the height of each column is the minimum value

CGFloat minheight = [self.cloumsheight[0] floatvalue];

Column number of the record

Nsinteger Cloum = 0;

Remove the lowest-height column

for (Nsinteger i=1; i<self.cloumsheight.count; i++) {

if (MinHeight > [self.cloumsheight[i] floatvalue]) {

MinHeight = [self.cloumsheight[i] floatvalue];

Cloum =i;

}

}

Calculate width

CGFloat W = (Self.collectionview.frame.size.width-coloummargindefault * (Coloumdefault + 1))/ColoumDefault;

Calculate height

CGFloat H = [self.delegate collectionView:self.collectionView Heightforitematindexpath:indexpath];

Calculates the x value of a cell

CGFloat X = Coloummargindefault + (W + coloummargindefault) *cloum;

Calculates the Y value of a cell

CGFloat Y = MinHeight + Rowmagindegault;

Attributes.frame = CGRectMake (X, Y, W, H);

Update an array that records the height of each column

[Self.cloumsheight Replaceobjectatindex:cloum withobject:@ (Cgrectgetmaxy (Attributes.frame))];

return attributes;

}

Returns the scroll range of the CollectionView

-(Cgsize) collectionviewcontentsize

{

CGFloat maxheight = 0;

for (int i=0; i<self.cloumsheight.count; i++) {

if (MaxHeight < [self.cloumsheight[i] floatvalue]) {

MaxHeight =[self.cloumsheight[i] Floatvalue];

}

}

Return Cgsizemake (0, MaxHeight);

}

@end

-----------------------------------------------------------------------------------------------so you can---------------------- --------------------------

Implementation of the waterfall stream for iOS development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.