瀑布流,也被稱爲瀑布流佈局。ios
行的一種頁面佈局,視覺表現爲參差不齊的多欄佈局,隨着頁面滾動欄向下滾動,這樣的佈局還會不斷載入數據塊並附加至當前尾部。算法
說明:(1)瀑布流每一個的寬度是同樣的,都是高度不同(2)補齊算法,哪裏比較短就補哪裏,不是簡單的從左到右排(兩列之間的差距愈來愈大)。佈局
在ios中眼下比較好的實現方式有2種。1.仿照UITableView思路本身定義ScrollView 2.使用UICollectionView,本身定義UICollectionViewLayout實現.字體
#import "ViewController.h" #import "WaterflowLayout.h" @interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, HMWaterflowLayoutDelegate> @property (nonatomic, weak) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *shops; @end @implementation ViewController - (NSMutableArray *)shops { if (_shops == nil) { self.shops = [NSMutableArray array]; } return _shops; } static NSString *const ID = @"shop"; - (void)viewDidLoad { [super viewDidLoad]; // 1.初始化數據 NSArray *shopArray = [Shop objectArrayWithFilename:@"1.plist"]; [self.shops addObjectsFromArray:shopArray]; WaterflowLayout *layout = [[WaterflowLayout alloc] init]; layout.delegate = self; // layout.sectionInset = UIEdgeInsetsMake(100, 20, 40, 30); // layout.columnMargin = 20; // layout.rowMargin = 30; // layout.columnsCount = 4; // 2.建立UICollectionView UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; collectionView.backgroundColor = [UIColor whiteColor]; collectionView.dataSource = self; collectionView.delegate = self; [collectionView registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellWithReuseIdentifier:ID]; [self.view addSubview:collectionView]; self.collectionView = collectionView; // 3.添加刷新控件 [self.collectionView addFooterWithTarget:self action:@selector(loadMoreShops)]; } - (void)loadMoreShops { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSArray *shopArray = [Shop objectArrayWithFilename:@"1.plist"]; [self.shops addObjectsFromArray:shopArray]; [self.collectionView reloadData]; [self.collectionView footerEndRefreshing]; }); } #pragma mark - <WaterflowLayoutDelegate> - (CGFloat)waterflowLayout:(WaterflowLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath { Shop *shop = self.shops[indexPath.item]; return shop.h / shop.w * width; } #pragma mark - <UICollectionViewDataSource> - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.shops.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { ShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; cell.shop = self.shops[indexPath.item]; return cell; } @end
#import <UIKit/UIKit.h> @class WaterflowLayout; @protocol WaterflowLayoutDelegate <NSObject> - (CGFloat)waterflowLayout:(WaterflowLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath; @end @interface WaterflowLayout : UICollectionViewLayout @property (nonatomic, assign) UIEdgeInsets sectionInset; /** 每一列之間的間距 */ @property (nonatomic, assign) CGFloat columnMargin; /** 每一行之間的間距 */ @property (nonatomic, assign) CGFloat rowMargin; /** 顯示多少列 */ @property (nonatomic, assign) int columnsCount; @property (nonatomic, weak) id<WaterflowLayoutDelegate> delegate; @end
#import "WaterflowLayout.h" //static const CGFloat ColumnMargin = 10; //static const CGFloat RowMargin = ColumnMargin; @interface WaterflowLayout(); /** 這個字典用來存儲每一列最大的Y值(每一列的高度) */ @property (nonatomic, strong) NSMutableDictionary *maxYDict; /** 存放所有的佈局屬性 */ @property (nonatomic, strong) NSMutableArray *attrsArray; @end @implementation WaterflowLayout - (NSMutableDictionary *)maxYDict { if (!_maxYDict) { self.maxYDict = [[NSMutableDictionary alloc] init]; } return _maxYDict; } - (NSMutableArray *)attrsArray { if (!_attrsArray) { self.attrsArray = [[NSMutableArray alloc] init]; } return _attrsArray; } - (instancetype)init { if (self = [super init]) { self.columnMargin = 10; self.rowMargin = 10; self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); self.columnsCount = 3; } return self; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } /** * 每次佈局以前的準備 */ - (void)prepareLayout { [super prepareLayout]; // 1.清空最大的Y值 for (int i = 0; i<self.columnsCount; i++) { NSString *column = [NSString stringWithFormat:@"%d", i]; self.maxYDict[column] = @(self.sectionInset.top); } // 2.計算所有cell的屬性 [self.attrsArray removeAllObjects]; NSInteger count = [self.collectionView numberOfItemsInSection:0]; for (int i = 0; i<count; i++) { UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; [self.attrsArray addObject:attrs]; } } /** * 返回所有的尺寸 */ - (CGSize)collectionViewContentSize { __block NSString *maxColumn = @"0"; [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) { if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) { maxColumn = column; } }]; return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom); } /** * 返回indexPath這個位置Item的佈局屬性 */ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { // 若是最短的那一列的第0列 __block NSString *minColumn = @"0"; // 找出最短的那一列 [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) { if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) { minColumn = column; } }]; // 計算尺寸 CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMargin)/self.columnsCount; CGFloat height = [self.delegate waterflowLayout:self heightForWidth:width atIndexPath:indexPath]; // 計算位置 CGFloat x = self.sectionInset.left + (width + self.columnMargin) * [minColumn intValue]; CGFloat y = [self.maxYDict[minColumn] floatValue] + self.rowMargin; // 更新這一列的最大Y值 self.maxYDict[minColumn] = @(y + height); // 建立屬性 UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attrs.frame = CGRectMake(x, y, width, height); return attrs; } /** * 返回rect範圍內的佈局屬性 */ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { return self.attrsArray; } @end
版權聲明:本文博主原創文章。博客,未經贊成不得轉載。atom