自定义一个继承自UICollectionViewLayout的layout文件:
var delegate:StrechDelete?
var currentOriginY:CGFloat= 215
var headerReferenceSize:CGSize?
var attributeArray:[UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
var insets:UIEdgeInsets=UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0)
override funcprepareLayout() {
super.prepareLayout()
self.attributeArray.removeAll()
let count =self.collectionView!.numberOfItemsInSection(0)
for j in 0..<count{
let attris =self.layoutAttributesForItemAtIndexPath(NSIndexPath(forRow: j, inSection: 0))
self.attributeArray.append(attris)
}
headerReferenceSize=CGSizeMake(screenW,200)
currentOriginY= (headerReferenceSize?.height)!+insets.top
}
override funcshouldInvalidateLayoutForBoundsChange(newBounds:CGRect) ->Bool{
return true
}
override funclayoutAttributesForItemAtIndexPath(indexPath:NSIndexPath)->UICollectionViewLayoutAttributes{
let width =screenW
//代理计算传入每个cell的高度
let height =self.delegate?.flowLayoutHeightForIndexPath(self, atIndexPath: indexPath)
let x:CGFloat= 0
let y =currentOriginY
// 创建属性
let attrs =UICollectionViewLayoutAttributes(forCellWithIndexPath:indexPath)
attrs.frame=CGRectMake(x, y, width, height!)
//通过代理获取从此cell开始是否有inset.top距离的cell间隔
let flag:Bool= (self.delegate?.flowLayoutAfterIsSeparatedForIndexPath(self, atIndexPath: indexPath))!
if flag {
currentOriginY+= (height!+insets.top)
}else{
currentOriginY+= height!
}
return attrs;
}
//很重要的方法,当边界发生改变时,是否应该刷新布局。如果YES则在边界变化(一般是scroll到其
//他地方)时,将重新计算需要的布局信息。
override funclayoutAttributesForElementsInRect(rect:CGRect) -> [UICollectionViewLayoutAttributes]? {
var attributesArray = [UICollectionViewLayoutAttributes]()
attributesArray.appendContentsOf(attributeArray)
let index2 =self.collectionView!.indexOfAccessibilityElement(UICollectionElementKindSectionHeader)
let attr2:UICollectionViewLayoutAttributes=self.collectionView!.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath:NSIndexPath(forItem: index2, inSection: 0))!
attributesArray.append(attr2)
return attributesArray
}
override funclayoutAttributesForSupplementaryViewOfKind(elementKind:String, atIndexPath indexPath:NSIndexPath) ->UICollectionViewLayoutAttributes? {
let attribute =UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, withIndexPath: indexPath)
if elementKind ==UICollectionElementKindSectionHeader{
let width =self.collectionView!.frame.width
attribute.frame=CGRect(x: 0,y: 0 ,width:width,height:headerReferenceSize!.height)
let offset =self.collectionView?.contentOffset
if offset?.y< 0 {//用户向下拉
let deltay =fabs((offset?.y)!)
//获取headView的frame
var rect = attribute.frame
//改变headView的高度,y值
rect.size.height+= deltay
rect.origin.y-= deltay
attribute.frame= rect
}
return attribute
}else{
//由于个人需要,没有构建footerVIew,所以在layout文件中并没有实现footerView的attribute构建
return nil
}
}
override funccollectionViewContentSize() ->CGSize{
returnCGSize(width: 0, height:screenH)
}
自定义的layout文件在uicollectionviewcontroller中实现delegate:StrechDelete的代理方法:
funcflowLayoutHeightForIndexPath(layout:SuzeeStrechCollectionViewLayout, atIndexPath:NSIndexPath) ->CGFloat{
return60
}
func flowLayoutAfterIsSeparatedForIndexPath(layout:SuzeeStrechCollectionViewLayout, atIndexPath:NSIndexPath) ->Bool{
let row = atIndexPath.row
switchrow {
case0:
return false
case1:
return true
default:
return true
}
}
,并重新定义collectionview的layout:
let layoutNew =***CollectionViewLayout()
//自定义layout创建delegate
layoutNew.delegate=self
self.collectionView!.collectionViewLayout= layoutNew
并实现
最终实现效果: