iOS Development--Advanced article--the basic use of flowing water layout uicollectionviewflowlayout

Source: Internet
Author: User

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

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.