美文网首页iOS学习录
UICollectionView 分页效果

UICollectionView 分页效果

作者: superlaing | 来源:发表于2018-05-11 13:52 被阅读22次

    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

    相关文章

      网友评论

        本文标题:UICollectionView 分页效果

        本文链接:https://www.haomeiwen.com/subject/qisfdftx.html