卡片式流水布局可以
##自定義布局-繼承UICollectionViewFlowLayout
###重寫prepareLayout方法
-作用:在這個方法中做一些初始化操作
-注意:一定要調(diào)用[super prepareLayout]
###重寫layoutAttributesForElementsInRect:方法
-作用:
-這個方法的返回值是個數(shù)組
-這個數(shù)組中存放的都是UICollectionViewLayoutAttributes對象
- UICollectionViewLayoutAttributes對象決定了cell的排布方式(frame等)
###重寫shouldInvalidateLayoutForBoundsChange:方法
-作用:如果返回YES,那么collectionView顯示的范圍發(fā)生改變時,就會重新刷新布局
-一旦重新刷新布局稠氮,就會按順序調(diào)用下面的方法:
- prepareLayout
- layoutAttributesForElementsInRect:
###重寫targetContentOffsetForProposedContentOffset:withScrollingVelocity:方法
-作用:返回值決定了collectionView停止?jié)L動時最終的偏移量(contentOffset)
-參數(shù):
- proposedContentOffset:原本情況下母蛛,collectionView停止?jié)L動時最終的偏移量
- velocity:滾動速率萧落,通過這個參數(shù)可以了解滾動的方向
.h
#import
@interfaceXMGLineLayout :UICollectionViewFlowLayout
@end
.m
- (instancetype)init
{
if(self= [superinit]) {
}
returnself;
}
/**
*當(dāng)collectionView的顯示范圍發(fā)生改變的時候春寿,是否需要重新刷新布局
*一旦重新刷新布局步脓,就會重新調(diào)用下面的方法:
1.prepareLayout
2.layoutAttributesForElementsInRect:方法
*/
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
returnYES;
}
/**
*用來做布局的初始化操作(不建議在init方法中進行布局的初始化操作)
*/
- (void)prepareLayout
{
[superprepareLayout];
//水平滾動
self.scrollDirection=UICollectionViewScrollDirectionHorizontal;
//設(shè)置內(nèi)邊距
CGFloatinset = (self.collectionView.frame.size.width-self.itemSize.width) *0.5;
self.sectionInset=UIEdgeInsetsMake(0, inset,0, inset);
}
/**
UICollectionViewLayoutAttributes *attrs;
1.一個cell對應(yīng)一個UICollectionViewLayoutAttributes對象
2.UICollectionViewLayoutAttributes對象決定了cell的frame
*/
/**
*這個方法的返回值是一個數(shù)組(數(shù)組里面存放著rect范圍內(nèi)所有元素的布局屬性)
*這個方法的返回值決定了rect范圍內(nèi)所有元素的排布(frame)
*/
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
//獲得super已經(jīng)計算好的布局屬性
NSArray*array = [superlayoutAttributesForElementsInRect:rect];
//計算collectionView最中心點的x值
CGFloatcenterX =self.collectionView.contentOffset.x+self.collectionView.frame.size.width*0.5;
//在原有布局屬性的基礎(chǔ)上堡僻,進行微調(diào)
for(UICollectionViewLayoutAttributes*attrsinarray) {
// cell的中心點x和collectionView最中心點的x值的間距
CGFloatdelta =ABS(attrs.center.x- centerX);
//根據(jù)間距值計算cell的縮放比例
CGFloatscale =1- delta /self.collectionView.frame.size.width;
//設(shè)置縮放比例
attrs.transform=CGAffineTransformMakeScale(scale, scale);
}
returnarray;
}
/**
*這個方法的返回值,就決定了collectionView停止?jié)L動時的偏移量
*/
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
//計算出最終顯示的矩形框
CGRectrect;
rect.origin.y=0;
rect.origin.x= proposedContentOffset.x;
rect.size=self.collectionView.frame.size;
//獲得super已經(jīng)計算好的布局屬性
NSArray*array = [superlayoutAttributesForElementsInRect:rect];
//計算collectionView最中心點的x值
CGFloatcenterX = proposedContentOffset.x+self.collectionView.frame.size.width*0.5;
//存放最小的間距值
CGFloatminDelta =MAXFLOAT;
for(UICollectionViewLayoutAttributes*attrsinarray) {
if(ABS(minDelta) >ABS(attrs.center.x- centerX)) {
minDelta = attrs.center.x- centerX;
}
}
//修改原有的偏移量
proposedContentOffset.x+= minDelta;
returnproposedContentOffset;
}
-文章轉(zhuǎn)自小碼哥