YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

iOS-不规则瀑布流-瀑布流图片-

iOS-不规则瀑布流-瀑布流图片-




//
// ViewController.m
// UI21_不规则瀑布流
//
// Created by YIem on 16/3/9.
// Copyright © 2016年 YIem. All rights reserved.
//

import "ViewController.h"

import "CollectionViewCell.h"

import "Model.h"

import "YTCollectionViewLayout.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, YTCollectionViewLayoutDelegate>
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation ViewController

// flowL.minimumInteritemSpacing = 10;
// flowL.minimumLineSpacing = 10;
// flowL.itemSize = CGSizeMake(120, 150);
// flowL.footerReferenceSize = CGSizeMake(10, 10);
// flowL.headerReferenceSize = CGSizeMake(10, 10);
// flowL.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
// // 宽度= (屏幕宽度 - 左间距 - 右间距 - 列间距* (列数 -1)) / 列数
// CGFloat width = ([UIScreen mainScreen].bounds.size.width - 10 - 10 -10 *(3 - 1)) / 3;
// flowL.itemSize = CGSizeMake(120, 100);

pragma - YTCollectionViewLayout

// YTCollectionViewLayout
YTCollectionViewLayout *flowL = [[YTCollectionViewLayout alloc] init];
flowL.interitemCount = 3;
flowL.interitemSpacing = 10;
flowL.lineSpacing = 10;
flowL.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
// 协议
flowL.delegate = self;

// flowL.scrollDirection = UICollectionViewScrollDirectionVertical;

UICollectionView *collectV = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowL];
collectV.backgroundColor = [UIColor yellowColor];
collectV.dataSource = self;
collectV.delegate = self;
[self.view addSubview:collectV];
[collectV release];

[self Data];
[collectV registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

}

}

pragma - (协议) 向YTCollectionViewLayout传人 计算好的 高度

}

@end




//
// YTCollectionViewLayout.h
// UI21_不规则瀑布流
//
// Created by YIem on 16/3/9.
// Copyright © 2016年 YIem. All rights reserved.
//

import <UIKit/UIKit.h>

@protocol YTCollectionViewLayoutDelegate <NSObject>

@end
@interface YTCollectionViewLayout : UICollectionViewLayout
// 协议 把高度传过来
@property (nonatomic, assign) id<YTCollectionViewLayoutDelegate> delegate;

@property (nonatomic, assign) CGFloat lineSpacing;
@property (nonatomic, assign) CGFloat interitemSpacing;
@property (nonatomic, assign) NSInteger interitemCount;
@property (nonatomic, assign) UIEdgeInsets sectionInset;
@end







//
// YTCollectionViewLayout.m
// UI21_不规则瀑布流
//
// Created by YIem on 16/3/9.
// Copyright © 2016年 YIem. All rights reserved.
//

import "YTCollectionViewLayout.h"

@interface YTCollectionViewLayout ()
@property (nonatomic, retain) NSMutableArray *itemArr;
@property (nonatomic, retain) NSMutableDictionary *heightDic;

@end
@implementation YTCollectionViewLayout

warning 2 - (系统方法) - 初始化开变量空间

warning 3 - (系统方法) - item布局前准备方法, 只在布局前调用一次

pragma - 计算坐标 及 大小

    
    // 宽度 = (屏幕宽度 - 左间距 - 右间距 - 列间距* (列数 -1)) / 列数
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - self.sectionInset.left - self.sectionInset.right - self.interitemSpacing * (self.interitemCount - 1)) / self.interitemCount;
    // X = 左间距 +(item宽度 + 列间距) * 最短item所在列号
    CGFloat x = self.sectionInset.left + (width + self.interitemSpacing) * [minHeightKey integerValue];
    // Y = 字典中对应的最小高度(注意; 更新最小高度时对应将行间距加入)
    CGFloat y =[[self.heightDic objectForKey:minHeightKey] doubleValue];
    // 高度
    // 创建
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
    // 高度 (计算好传过来的)
    CGFloat height = [self.delegate heightWithIndex:indexPath width:width];
    
    // 更新高度
    // 
    NSNumber *heightVal = [NSNumber numberWithDouble:(height + self.lineSpacing) + y];
    [self.heightDic setObject:heightVal forKey:minHeightKey];
    
    
    

    //
    UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    attribute.frame = CGRectMake(x , y, width, height);

    
    
    [self.itemArr addObject:attribute];
}

}

warning 1 -(系统方法) - 返回给系统所有的item大小及坐标

warning 4 - (系统方法) - 设置滚动范围

@end




//
// Model.h
// UI21_不规则瀑布流
//
// Created by YIem on 16/3/9.
// Copyright © 2016年 YIem. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Model : NSObject
@property (nonatomic, copy) NSString *thumbURL;
@property (nonatomic, copy) NSString *width;
@property (nonatomic, copy) NSString *height;
@end



//
// Model.m
// UI21_不规则瀑布流
//
// Created by YIem on 16/3/9.
// Copyright © 2016年 YIem. All rights reserved.
//

import "Model.h"

@implementation Model

@end




//
// CollectionViewCell.h
// UI21_不规则瀑布流
//
// Created by YIem on 16/3/9.
// Copyright © 2016年 YIem. All rights reserved.
//

import <UIKit/UIKit.h>

@class Model;
@interface CollectionViewCell : UICollectionViewCell

@property (nonatomic, retain) Model *model;
@end





//
// CollectionViewCell.m
// UI21_不规则瀑布流
//
// Created by YIem on 16/3/9.
// Copyright © 2016年 YIem. All rights reserved.
//

import "CollectionViewCell.h"

import "UIImageView+WebCache.h"

import "Model.h"

@interface CollectionViewCell ()
@property (nonatomic, retain) UIImageView *imageV;
@end
@implementation CollectionViewCell

}
// 当Frame发生改变时会触发
// 当frame相同时不会触发
// 在appapplyLayoutAttributes之后调用, 最后调用

}
// 布局时会触发 (必然触发)
//- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
//{
// [super applyLayoutAttributes:layoutAttributes];
// // 防止imageV 大小与重用池取出的cell不匹配
// self.imageV.frame = self.contentView.bounds;
//}

@end


当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »