需求
在当前的移动端开发中,我们经常可以看见集合视图需要横向布局分页的场景。例如:
微信的表情
美团外卖的首页
需求实现分析
//预布局方法 布局相关代码可放在此处
override func prepare() {
}
/**
返回true只要显示的边界发生改变就重新布局:(默认是false)
内部会重新调用prepareLayout和调用
layoutAttributesForElementsInRect方法获得部分cell的布局属性
*/
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true;
}
/*
根据indexPath去对应的UICollectionViewLayoutAttributes 这个是取值的,要重写,在移动删除的时候系统会调用改方法重新去UICollectionViewLayoutAttributes然后布局
*/
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return super.layoutAttributesForItem(at: indexPath);
}
//返回当前的ContentSize
override open var collectionViewContentSize: CGSize {
return CGSize(width: (self.collectionView?.width)! * CGFloat(self.collectionView!.numberOfSections), height: self.collectionView!.height);
}