Use Uicollectionviewflowlayout for layout in Uicollectionview (imitate Apple album)

Source: Internet
Author: User

It is now known that when initializing the Uicollectionview, you must pass in a layout object to manage it. This is also the obvious difference between CollectionView and TableView, through the collectionviewlayout, can be more powerful control of the CollectionView.

Customize a uicollectionviewflowlayout, re-inside the several methods.

Method One: Each sliding CollectionView calls this method and asks whether to re-layout. If Returen Yes, method Two and method three are called.

-(BOOL) Shouldinvalidatelayoutforboundschange: (CGRect) newbounds{

//

return YES;

}

Method Two:

-(void) preparelayout{

[Super Preparelayout];

Here, you can make some other changes before layout. or not doing anything.

}

Method Three: In this method, you can obtain a layout attribute to all cells that are ready to be refreshed: uicollectionviewlayoutattributes, through this attributes, you can change the cell's Size,frame, The layout properties of Alpha and so on.

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

Call the method of the parent class to get.

Nsarray *array = [Super Layoutattributesforelementsinrect:rect];

Calculates the X-value of the CollectionView center point

CGFloat CenterX = self.collectionview.contentoffset.x + self.collectionView.frame.size.width * 0.5;

Based on the original layout properties, fine-tune

For (uicollectionviewlayoutattributes *attrs in array) {

The space between the X-values of the center point X and the CollectionView center point of the cell

CGFloat delta = ABS (Attrs.center.x-centerx);

Calculates the scale of the cell based on the spacing value

CGFloat scale = 1-delta/self.collectionview.frame.size.width;

Set the zoom ratio

Attrs.transform = Cgaffinetransformmakescale (scale, scale);

}

return array;

}

Method Four: After each hand slide, it will be called. Is the instant call that the hand leaves the screen. Return a final offset, if you want to change it, set it here. If you do not change it, return directly to Proposedcontentoffset.

-(Cgpoint) Targetcontentoffsetforproposedcontentoffset: (cgpoint) Proposedcontentoffset withScrollingVelocity: ( Cgpoint) velocity{

return proposedcontentoffset;

}

Here is the one I made, the cell did not put the picture up, directly with the color tag: the same reason, waterfall flow is so. The wrong place, hope to advise

Use Uicollectionviewflowlayout for layout in Uicollectionview (imitate Apple album)

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.