瀑布流学习、 点击状态栏返回顶部、 点击按钮返回顶部、Label透明度字体跟着变浅的解决方法

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;
 }
 */


#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 ;
}

@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 . scrollsToTop = TRUE ;



——————————————————————Label透明度字体跟着变浅的解决方法——————————————




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值