Custom UICollectinviewFlowLayout, that is, implementing waterfall stream. Custom ios flowlayout

Source: Internet
Author: User

Custom UICollectinviewFlowLayout, that is, implementing waterfall stream. Custom ios flowlayout

To display different effects through irregular grid distribution. Because the layout must be specified and displayed in the set view, you can customize the layout to achieve the waterfall flow effect.

// Create the layout object WaterFlowLayout * flowLayout = [[WaterFlowLayout alloc] init]; flowLayout. delegate = self; flowLayout. numberOfColumn = 3; // create set view UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame: [UIScreen mainScreen]. bounds collectionViewLayout: flowLayout];

There are four methods for the system's built-in layout: item size, partition interval, minimum row spacing, and gap size between items.

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>@optional- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

Therefore, customize FlowLayout and define the Protocol to define these methods.

@ Protocol WaterFlowLayoutDelegate <NSObject> // key method. This method returns the size of the original image in each item. // (CGSize) collectionView (UICollectionView *) collectionView layout :( WaterFlowLayout *) collectionViewLayout layout :( NSIndexPath *) indexPath; // partition interval-(partition) collectionView :( UICollectionView *) collectionView layout :( WaterFlowLayout *) collectionViewLayout layout :( NSInteger) section; // obtain the gap size between items-(CGFloat) collectionView :( UICollectionView *) collectionView layout :( WaterFlowLayout *) collectionViewLayout minimumLineSpacingForSectionAtIndex :( NSInteger) section; // CGFloat) collectionView :( UICollectionView *) collectionView layout :( WaterFlowLayout *) collectionViewLayout layout :( NSInteger) section; @ end @ interface WaterFlowLayout: Total number of columns of UICollectionViewLayout // waterfall stream @ property (nonatomic, assign) NSUInteger numberOfColumn; @ property (nonatomic, assign) id <WaterFlowLayoutDelegate> delegate;

As shown in the figure, the minimum height of the item is used as the starting point of the next item column.

@ Interface WaterFlowLayout () // store the height of each column @ property (nonatomic, retain) NSMutableArray * columnHeightsArray; // stores the attributes of each item, including frame and subscript @ property (nonatomic, retain) NSMutableArray * attributesArray; @ end @ implementation WaterFlowLayout // method for obtaining the minimum height-(CGFloat) minHeight {CGFloat min = 100000; for (NSNumber * height in _ columnHeightsArray) {CGFloat h = [height floatValue]; if (min> h) {min = h ;}} return min ;}// obtain the maximum value-(CGFloat) maxHeight {CGFloat max = 0; for (NSNumber * height in _ columnHeightsArray) {CGFloat h = [height floatValue]; if (max 

Note: The implementation of the last method, namely-(NSArray *) layoutAttributesForElementsInRect :( CGRect) rect. If this method is not written, the set view cannot be displayed, this method re-tells the set view about each item saved for display.

Related Article

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.