iOS学习录

UICollectionView 分页效果

2018-05-11  本文已影响22人  superlaing

1、分页宽度和屏幕宽度相等:设置PageEnable为YES即可;
2、分页宽度和屏幕宽度不相等:自定义FlowLayout

- (void)awakeFromNib {
   [super awakeFromNib];
   [self initSettings];
}

- (instancetype)init {
   self = [super init];
   if (self) {
       [self initSettings];
   }
   return self;
}

- (void)initSettings {
   //默认flowLayout,可在collectionView自定义以下参数
   self.itemSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
   self.minimumInteritemSpacing = 10.0;
   self.minimumLineSpacing = 10.0;
   self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
   self.sectionInset = UIEdgeInsetsMake(0, 20.0, 0, 20.0);
}

- (CGFloat)pageWidth {
   return self.itemSize.width + self.minimumLineSpacing;
}

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
   CGFloat rawPageValue = self.collectionView.contentOffset.x / self.pageWidth;
   CGFloat currentPage = (velocity.x > 0.0) ? floor(rawPageValue) : ceil(rawPageValue);
   CGFloat nextPage = (velocity.x > 0.0) ? ceil(rawPageValue) : floor(rawPageValue);
   
   BOOL pannedLessThanAPage = fabs(1 + currentPage - rawPageValue) > 0.5;
   BOOL flicked = fabs(velocity.x) > [self flickVelocity];
   if (pannedLessThanAPage && flicked) {
       proposedContentOffset.x = nextPage * self.pageWidth;
   } else {
       proposedContentOffset.x = round(rawPageValue) * self.pageWidth;
   }
   return proposedContentOffset;
}

- (CGFloat)flickVelocity {
   return 0.3;
}

参考:https://stackoverflow.com/questions/13492037/targetcontentoffsetforproposedcontentoffsetwithscrollingvelocity-without-subcla

上一篇下一篇

猜你喜欢

热点阅读