1、使用collectionView实现瀑布流布局:
(1)该方法准备布局初始化:
override func prepare() {}
(2)用于设置ContentView的ContentViewSize//swiftt3.0 废除了该方法:override func collectionViewContentSize()->CGSize{ }
override var collectionViewContentSize: CGSize{}
(3)返回Rect内的item attributes
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {}
(4) // 设置Item的尺寸
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {}
2、具体的使用方式:
prepare 的实现:
override func prepare() {
super.prepare();
//初始化每一列的最大值maxYDic:key---第几列,value---该列的最大Y值
for index in 0..<columnCount{
self.maxYDic[(index)] = (self.sectionInset.top);
}
//获取item 的数目
let itemCount:NSInteger = (self.collectionView?.numberOfItems(inSection: 0))!;
self.attributesArray=[];
//为每一个item创建一个attributes并存入数组
for i in 0..<itemCount{
let attributes:UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath.init(row: i, section: 0))!;
self.attributesArray.append(attributes);
}
}
ContentViewSize的实现:
//swift3.0 废除了该方法:override func collectionViewContentSize()->CGSize{ }
override var collectionViewContentSize: CGSize{
get{
return CGSize(width: (self.collectionView?.bounds.width)!, height: self.maxYHeight())
}
set{
return self.collectionViewContentSize=newValue;
}
}
layoutAttributesForElements的实现
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return self.attributesArray;
}
layoutAttributesForItem的实现:
// 设置Item的尺寸
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
super.layoutAttributesForItem(at: indexPath);
let collectionViewWidth:CGFloat = self.collectionView!.frame.size.width;
let size = setSize();
let tmpafloat:CGFloat = CGFloat((self.columnCount - 1) * self.columnSpacing);//间隔总宽度
let itemWidth:CGFloat = CGFloat(collectionViewWidth - (self.sectionInset.left) - (self.sectionInset.right) - tmpafloat) / CGFloat( self.columnCount);//
let nimIndex = self.minHeight();
// 根据最短列的列数计算item的x值
let itemX:CGFloat = self.sectionInset.left + (CGFloat(self.columnSpacing) + itemWidth) * nimIndex;
// item的y值 = 最短列的最大y值 + 行间距
let tmpmin:NSNumber = self.maxYDic.object(forKey: nimIndex) as! NSNumber;
let itemY:CGFloat = CGFloat(truncating: tmpmin)+self.rowSpacing;
let tmpWidth:CGFloat = CGFloat(collectionViewWidth - 20.0)/2.0;
let itemHeight:CGFloat = (tmpWidth * size[indexPath.row].size.height / size[indexPath.row].size.width) ;
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath);//没有这一行,会报错:intRect问题
attributes.frame=CGRect.init(x: itemX, y: itemY, width: itemWidth, height: itemHeight);
attributes.indexPath=indexPath;
//更新字典中的最短列的最大y值
self.maxYDic[nimIndex] = (attributes.frame.maxY);
return attributes;
}
获取最大Y
//获取最大Y值
func maxYHeight() -> CGFloat {
//假设最大值的key=0,
let indexOne:NSNumber = (0);
var maxValue = self.maxYDic.object(forKey: indexOne) as! NSNumber;
for (key,_) in self.maxYDic {
let tmpValue:NSNumber = self.maxYDic.object(forKey: key) as! NSNumber;
if tmpValue.intValue > maxValue.intValue{
maxValue = tmpValue;
}
}
return CGFloat(maxValue.floatValue) + (self.sectionInset.bottom);
}
获取最小高度:
func minHeight() -> CGFloat {
let indexOne:NSNumber = (0);
var minValue = self.maxYDic.object(forKey: indexOne) as! NSNumber;
var minKey:NSNumber = (0);
for (key,_) in self.maxYDic {
let tmpValue:NSNumber = self.maxYDic.object(forKey: key) as! NSNumber;
if minValue.intValue > tmpValue.intValue{
minValue = tmpValue;
minKey = key as! NSNumber;
}
}
return CGFloat(minKey.floatValue);
}
}
基本变量的设置
var columnCount:NSInteger = 2 // 总列数
var columnSpacing:NSInteger = 10 // 列间距
var rowSpacing:CGFloat = 10 // 行间距
var sectionInset:UIEdgeInsets = UIEdgeInsetsMake(0, 0 , 0, 0) //section到contentView的边距
var maxYDic:NSMutableDictionary = NSMutableDictionary() //保存每一列最大y值的数组
var attributesArray:Array<UICollectionViewLayoutAttributes>! //保存每一个item的attributes的数组
var setSize:()->(Array<UIImage>) = {
return []
}
调用只使用以下方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageArr.count;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:TmpCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! TmpCollectionViewCell
cell.tmpImgView.image=self.imageArr[indexPath.row];
return cell;
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1 ;
}
调用的初始化:
var myCollectionView:UICollectionView?;
var imageArr:Array<UIImage>!
var maskView: UIView!
var cellRect: CGRect!
var changeRect: CGRect!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let pathStr:String = Bundle.main.path(forResource: "ImagePlist", ofType: "plist")!;
let tmpdic:NSDictionary = NSDictionary.init(contentsOfFile: pathStr)!;
let layout = CustomCollectionViewLayout();
layout.columnCount = 3;//共多少列
layout.columnSpacing = 10;//列间距
layout.rowSpacing = 10;//行间距
layout.sectionInset = UIEdgeInsetsMake(5, 10 , 5, 10);//内边距
imageArr = []
let namesImageArr:Array<String> = (tmpdic.object(forKey:"ImageSets"))! as! Array
for i in 1..<namesImageArr.count {
let image = UIImage(named:namesImageArr[i])
self.imageArr.append(image!)
}
layout.setSize = {
return self.imageArr;
}
self.myCollectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout);
self.myCollectionView?.backgroundColor=UIColor.brown;
// Register cell classes
let tmpnib:UINib = UINib.init(nibName: reuseIdentifier, bundle: nil);
self.myCollectionView?.register(tmpnib, forCellWithReuseIdentifier: reuseIdentifier);
self.myCollectionView?.delegate = self;
self.myCollectionView?.dataSource = self;
self.view.addSubview(self.myCollectionView!);
}