Skip to content

Commit

Permalink
修改滑动
Browse files Browse the repository at this point in the history
  • Loading branch information
iosLiuPeng committed Dec 17, 2018
1 parent 1072708 commit 55c4d5d
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions Classes/MJCenterLargenFlowLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

#import "MJCenterLargenFlowLayout.h"

@interface MJCenterLargenFlowLayout ()
@property (nonatomic, assign) CGFloat lastOffsetX;///< 上次滑动结束时,x轴偏移量
@end

@implementation MJCenterLargenFlowLayout
-(void)prepareLayout {
// 所有个数
Expand Down Expand Up @@ -59,18 +55,18 @@ -(void)prepareLayout {
// 每次都有图片居中
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
// 不用系统的预计停止位置,改用当前collection的偏移量 + 速度,保证一次只能滑一页
CGRect current = CGRectMake(self.collectionView.contentOffset.y, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
CGRect current = CGRectMake(self.collectionView.contentOffset.x, 0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
UICollectionViewLayoutAttributes *attr = [super layoutAttributesForElementsInRect:current].firstObject;
CGFloat itemWidth = attr.size.width;

if (velocity.x < -0.2 || velocity.x > 0.2) {
// 有一定速度时,才重新估算。
if (velocity.x > 0) {
// 向左
proposedContentOffset = CGPointMake(_lastOffsetX + itemWidth, 0);
proposedContentOffset = CGPointMake(self.collectionView.contentOffset.x + itemWidth / 2, 0);
} else {
// 向右
proposedContentOffset = CGPointMake(_lastOffsetX - itemWidth, 0);
proposedContentOffset = CGPointMake(self.collectionView.contentOffset.x - itemWidth / 2, 0);
}
}

Expand Down Expand Up @@ -108,7 +104,6 @@ - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentO
_selectBlock(index);
}

_lastOffsetX = proposedContentOffset.x;
return proposedContentOffset;
}

Expand Down

0 comments on commit 55c4d5d

Please sign in to comment.