self.dict enumerateKeysAndObjectsUsingBlocking:^(NSString *key, NSNumber *maxY, BOOL *stop){
};
———————————————————————————————瀑布流———————————
#import
<UIKit/UIKit.h>
@class CKWaterFlowLayout;
// 唯一的代理方法
@protocol CKWaterFlowLayoutDelegate <NSObject>
- (CGFloat)waterFlowLayout:(CKWaterFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath;
@end
@interface CKWaterFlowLayout : 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<CKWaterFlowLayoutDelegate> delegate;
@end
// 瀑布流使用指南
/**
- (void)addCollectionView
{
CKWaterFlowLayout *layout = [[CKWaterFlowLayout alloc]init];
layout.delegate = self;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.columnMargin = 10;
layout.rowMargin = 10;
layout.columnsCount = 3;
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerNib:[UINib nibWithNibName:@"WaterFlowCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WaterFlowCollectionViewCell"];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
}
#pragma mark -- CKwaterFlowLayoutDelegate
- (CGFloat)waterFlowLayout:(CKWaterFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath
{
ImageModel *model = self.dataArray[indexPath.item];
return model.y / model.x *width;
}
@class CKWaterFlowLayout;
// 唯一的代理方法
@protocol CKWaterFlowLayoutDelegate <NSObject>
- (CGFloat)waterFlowLayout:(CKWaterFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath;
@end
@interface CKWaterFlowLayout : 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<CKWaterFlowLayoutDelegate> delegate;
@end
// 瀑布流使用指南
/**
- (void)addCollectionView
{
CKWaterFlowLayout *layout = [[CKWaterFlowLayout alloc]init];
layout.delegate = self;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.columnMargin = 10;
layout.rowMargin = 10;
layout.columnsCount = 3;
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerNib:[UINib nibWithNibName:@"WaterFlowCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WaterFlowCollectionViewCell"];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
}
#pragma mark -- CKwaterFlowLayoutDelegate
- (CGFloat)waterFlowLayout:(CKWaterFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath
{
ImageModel *model = self.dataArray[indexPath.item];
return model.y / model.x *width;
}
*/
#import
"CKWaterFlowLayout.h"
@interface CKWaterFlowLayout()
// 这个字典用来存放每一列最大的Y值(每一列的高度)
@property (nonatomic,retain)NSMutableDictionary *maxYDict;
// 用来存放所有的布局属性
@property (nonatomic,retain)NSMutableArray *attresArray;
@end
@implementation CKWaterFlowLayout
// 所有的懒加载
- (NSMutableDictionary *)maxYDict
{
if (!_maxYDict) {
self.maxYDict = [[NSMutableDictionary alloc]init];
}
return _maxYDict;
}
- (NSMutableArray *)attresArray
{
if (!_attresArray) {
self.attresArray = [[NSMutableArray alloc]init];
}
return _attresArray;
}
- (instancetype)init
{
// 默认值是10 10 3 ,可以在控制器里面设置
if (self = [super init]) {
self.columnMargin = 10;
self.rowMargin = 10;
self.columnsCount = 3;
}
return self;
}
// 允许滑动
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
// 每次布局前的准备
- (void)prepareLayout
{
[super prepareLayout];
for (int i = 0; i < self.columnsCount; i++) {
NSString *column = [NSString stringWithFormat:@"%d",i];
self.maxYDict[column] = @(self.sectionInset.top);
}
[self.attresArray removeAllObjects];
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i < count; i++) {
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
[self.attresArray addObject:attrs];
}
}
// 返回所有的尺寸
- (CGSize)collectionViewContentSize
{
// 假装第0列最大
__block NSString *maxColumn = @"0";
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL * _Nonnull stop) {
if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) {
maxColumn = column;
}
}];
return CGSizeMake(0,[self.maxYDict[maxColumn] floatValue]);
}
// 计算返回所有indexPath这个位置Item的布局属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 假装第0列最短
__block NSString *minColumn = @"0";
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL * _Nonnull 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;
self.maxYDict[minColumn] = @(y + height);
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attrs.frame = CGRectMake(x, y, width, height);
return attrs;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.attresArray;
}
@interface CKWaterFlowLayout()
// 这个字典用来存放每一列最大的Y值(每一列的高度)
@property (nonatomic,retain)NSMutableDictionary *maxYDict;
// 用来存放所有的布局属性
@property (nonatomic,retain)NSMutableArray *attresArray;
@end
@implementation CKWaterFlowLayout
// 所有的懒加载
- (NSMutableDictionary *)maxYDict
{
if (!_maxYDict) {
self.maxYDict = [[NSMutableDictionary alloc]init];
}
return _maxYDict;
}
- (NSMutableArray *)attresArray
{
if (!_attresArray) {
self.attresArray = [[NSMutableArray alloc]init];
}
return _attresArray;
}
- (instancetype)init
{
// 默认值是10 10 3 ,可以在控制器里面设置
if (self = [super init]) {
self.columnMargin = 10;
self.rowMargin = 10;
self.columnsCount = 3;
}
return self;
}
// 允许滑动
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
// 每次布局前的准备
- (void)prepareLayout
{
[super prepareLayout];
for (int i = 0; i < self.columnsCount; i++) {
NSString *column = [NSString stringWithFormat:@"%d",i];
self.maxYDict[column] = @(self.sectionInset.top);
}
[self.attresArray removeAllObjects];
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (int i = 0; i < count; i++) {
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
[self.attresArray addObject:attrs];
}
}
// 返回所有的尺寸
- (CGSize)collectionViewContentSize
{
// 假装第0列最大
__block NSString *maxColumn = @"0";
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL * _Nonnull stop) {
if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) {
maxColumn = column;
}
}];
return CGSizeMake(0,[self.maxYDict[maxColumn] floatValue]);
}
// 计算返回所有indexPath这个位置Item的布局属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 假装第0列最短
__block NSString *minColumn = @"0";
[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL * _Nonnull 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;
self.maxYDict[minColumn] = @(y + height);
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attrs.frame = CGRectMake(x, y, width, height);
return attrs;
}
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.attresArray;
}
@end
————————————————————————回到顶部——————————————————————
UIBarButtonItem
*rightButtonItem = [[UIBarButtonItem
alloc]initWithTitle:@"返回顶部"
style:(UIBarButtonItemStylePlain)
target:self
action:@selector(rightButtonItemAction)];
self.navigationItem.rightBarButtonItem
= rightButtonItem;
- (void)rightButtonItemAction
{
[self.collectionView setContentOffset:CGPointMake(0, -64) animated:YES];
{
[self.collectionView setContentOffset:CGPointMake(0, -64) animated:YES];
}
点击状态栏返回顶部
self.collectionView.scrollsToTop
=
TRUE;
——————————————————————Label透明度字体跟着变浅的解决方法——————————————