最近項(xiàng)目有個(gè)功能是照片瀏覽器,當(dāng)預(yù)覽照片時(shí)裙戏,可以整屏左右滑動(dòng)乘凸,一想到整屏滑動(dòng),自然而然就想到了scrollview的pageEnable屬性累榜,于是就興致勃勃的把該屬性設(shè)為YES营勤,可是顯示出來的效果并不盡如人意,cell和cell滾動(dòng)之后總會(huì)有一定單位的偏差壹罚,于是又想到了另外一個(gè)直接設(shè)置scrollView的偏移量的做法葛作,然并卵。
在網(wǎng)上搜索解決辦法猖凛,無意戳進(jìn)了這個(gè)鏈接http://www.reibang.com/p/8de72cb2aae5赂蠢,在通讀完整篇文章后,我對(duì)評(píng)論區(qū)中提到的解決辦法:重寫flowLayout里的targetContentOffsetForProposedContentOffset這個(gè)代理方法來實(shí)現(xiàn)該效果比較感興趣辨泳,于是就依照此原理進(jìn)行了編寫虱岂。
因?yàn)樘O果系統(tǒng)已經(jīng)提供給我們了一個(gè)流水布局,所以新建一個(gè)類繼承于UICollectionViewFlowLayout菠红。
接下來需要重寫父類中的一些方法第岖,自定義UICollectionViewLayout時(shí)常用的方法有如下一些
//預(yù)布局方法 所有的布局應(yīng)該寫在這里面
- (void)prepareLayout
//此方法應(yīng)該返回當(dāng)前屏幕正在顯示的視圖(cell 頭尾視圖)的布局屬性集合(UICollectionViewLayoutAttributes 對(duì)象集合)
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
//根據(jù)indexPath去對(duì)應(yīng)的UICollectionViewLayoutAttributes 這個(gè)是取值的,要重寫试溯,在移動(dòng)刪除的時(shí)候系統(tǒng)會(huì)調(diào)用改方法重新去UICollectionViewLayoutAttributes然后布局
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
//返回當(dāng)前的ContentSize
- (CGSize)collectionViewContentSize
//是否重新布局
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
//這4個(gè)方法用來處理插入蔑滓、刪除和移動(dòng)cell時(shí)的一些動(dòng)畫 瀑布流代碼詳解
- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems
- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
- (nullable UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
- (void)finalizeCollectionViewUpdates
//9.0之后處理移動(dòng)相關(guān)
- (UICollectionViewLayoutInvalidationContext *)invalidationContextForInteractivelyMovingItems:(NSArray<nsindexpath *> *)targetIndexPaths withTargetPosition:(CGPoint)targetPosition previousIndexPaths:(NSArray<nsindexpath *> *)previousIndexPaths previousPosition:(CGPoint)previousPosition NS_AVAILABLE_IOS(9_0)
- (UICollectionViewLayoutInvalidationContext *)invalidationContextForEndingInteractiveMovementOfItemsToFinalIndexPaths:(NSArray<nsindexpath *> *)indexPaths previousIndexPaths:(NSArray<nsindexpath *> *)previousIndexPaths movementCancelled:(BOOL)movementCancelled NS_AVAILABLE_IOS(9_0)</nsindexpath *></nsindexpath *></nsindexpath *></nsindexpath *>
我們選擇重寫這些方法中的4個(gè)方法即可完成需求
1.- (void)prepareLayout
- (void)prepareLayout {
[super prepareLayout];
// 設(shè)置滾動(dòng)方向
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// 設(shè)置collectionView的cell大小
self.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}
2.- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
3.-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *array = [super layoutAttributesForElementsInRect:rect];
return array;
}
4.- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
//1. 獲取UICollectionView停止的時(shí)候的可視范圍
CGRect rangeFrame;
rangeFrame.size = self.collectionView.frame.size;
rangeFrame.origin = proposedContentOffset;
NSArray *array = [self layoutAttributesForElementsInRect:rangeFrame];
//2. 計(jì)算在可視范圍的距離中心線最近的Item
CGFloat minCenterX = CGFLOAT_MAX;
CGFloat collectionCenterX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;
for (UICollectionViewLayoutAttributes *attrs in array) {
if(ABS(attrs.center.x - collectionCenterX) < ABS(minCenterX)){
minCenterX = attrs.center.x - collectionCenterX;
}
}
//3. 補(bǔ)回ContentOffset,則正好將Item居中顯示
CGFloat proposedX = proposedContentOffset.x + minCenterX;
// 滑動(dòng)一屏?xí)r的偏移量
CGFloat mainScreenBounds = [UIScreen mainScreen].bounds.size.width + 10;
// 正向滑動(dòng)僅滑動(dòng)一屏
if (proposedX - self.lastProposedContentOffset >= mainScreenBounds) {
proposedX = mainScreenBounds + self.lastProposedContentOffset;
}
// 反向滑動(dòng)僅滑動(dòng)一屏
if (proposedX - self.lastProposedContentOffset <= -mainScreenBounds) {
proposedX = -mainScreenBounds + self.lastProposedContentOffset;
}
self.lastProposedContentOffset = proposedX;
CGPoint finialProposedContentOffset = CGPointMake(proposedX, proposedContentOffset.y);
return finialProposedContentOffset;
}
demo鏈接:https://github.com/TMMMMMS/CollectionViewPageEnable.git
(在編輯這篇文章臨近末尾時(shí)遇绞,突然想到好像使用SDCycleScrollview也可以實(shí)現(xiàn)此功能)