An article I've seen before
The use of CollectionView has been summed up very well: "IOS6 new features: Uicollectionview Introduction"
Water layout in the current application is very common, the simple study of the next, to achieve the following functions
Then I will briefly introduce the following uicollectionviewflowlayout commonly used several methods, through these methods can do the above effect
1. Layout
/**/-(void) preparelayout{ [Super Preparelayout];}
Set here to scroll horizontally
Self.scrolldirection = Uicollectionviewscrolldirectionhorizontal;
2. Controls within the Rect range are arranged
/**/* * *-(Nsarray *) Layoutattributesforelementsinrect: ( CGRect) rect{ // get Super Calculated layout properties nsarray *array = [Super Layoutattributesforelementsinrect:rect]; return array; }
In this method, we can get the layout properties of the system and then traverse and fine-tune the cell.
//get the layout properties that Super has calculatedNsarray *array =[Super Layoutattributesforelementsinrect:rect]; //calculates the X-value of the CollectionView center pointCGFloat CenterX = self.collectionview.contentoffset.x + self.collectionView.frame.size.width *0.5; //based on the original layout properties, fine-tune for(Uicollectionviewlayoutattributes *attrsinchArray) { //The space between the X-values of the center point X and the CollectionView center point of the cellCGFloat delta = ABS (Attrs.center.x-CenterX); //calculates the scale of the cell based on the spacing valueCGFloat scale =1-Delta/Self.collectionView.frame.size.width; //set the zoom ratioAttrs.transform =Cgaffinetransformmakescale (scale, scale); }
3. Re-refresh the layout
/**/- (BOOL) Shouldinvalidatelayoutforboundschange: (CGRect ) newbounds{ return YES;}
Because you need to change the attrs.transform in real time after scrolling , the return yes represents the effect of calling 1 when the rect changes and the 2 method re-layout to zoom in when scrolling to the middle.
4, CollectionView the offset when the scroll is stopped
/**/- (Cgpoint) Targetcontentoffsetforproposedcontentoffset: (Cgpoint) Proposedcontentoffset withscrollingvelocity: (cgpoint) velocity{ // Proposedcontentoffset Expected position (at the end of scrolling) return proposedcontentoffset;}
In this method, you can determine which cell center point is nearest when the scrolling is really stopped, and calculate the spacing (positive or negative) of the center point in the nearest cell, plus the expected contentoffset of the system to modify the actual roll-in position.
Core code:
//calculate the final display of the rectangle boxCGRect rect; RECT.ORIGIN.Y=0; Rect.origin.x=Proposedcontentoffset.x; Rect.size=self.collectionView.frame.size; //get the layout properties that Super has calculatedNsarray *array =[Super Layoutattributesforelementsinrect:rect]; //calculates the X-value of the CollectionView center pointCGFloat CenterX = proposedcontentoffset.x + self.collectionView.frame.size.width *0.5; //Store the minimum spacing valueCGFloat Mindelta =maxfloat; for(Uicollectionviewlayoutattributes *attrsinchArray) { if(ABS (Mindelta) > abs (Attrs.center.x-CenterX)) {Mindelta= Attrs.center.x-CenterX; } } //Modify the original offsetProposedcontentoffset.x + =Mindelta; returnProposedcontentoffset;
iOS Development--Advanced article--the basic use of flowing water layout uicollectionviewflowlayout