Skip to content
No description or website provided.
Branch: master
Clone or download
Latest commit e1ff7db Feb 7, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
UICollectionViewCellWidthPage.xcodeproj DEMO演示 Feb 7, 2017
UICollectionViewCellWidthPage img Feb 7, 2017
1486452112.gif
README.md Update README.md Feb 7, 2017

README.md

说明

需求:

第一个和最后一个item留空白,滚动居中,两边显示前后的item

效果演示:

关键代码:

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)offset
                                 withScrollingVelocity:(CGPoint)velocity {

    CGRect cvBounds = self.collectionView.bounds;
    CGFloat halfWidth = cvBounds.size.width * 0.5f;
    CGFloat proposedContentOffsetCenterX = offset.x + halfWidth;

    NSArray* attributesArray = [self layoutAttributesForElementsInRect:cvBounds];

    UICollectionViewLayoutAttributes* candidateAttributes;
    for (UICollectionViewLayoutAttributes* attributes in attributesArray) {

        // == Skip comparison with non-cell items (headers and footers) == //
        if (attributes.representedElementCategory !=
            UICollectionElementCategoryCell) {
            continue;
        }

        // == First time in the loop == //
        if(!candidateAttributes) {
            candidateAttributes = attributes;
            continue;
        }

        if (fabs(attributes.center.x - proposedContentOffsetCenterX) <
            fabs(candidateAttributes.center.x - proposedContentOffsetCenterX)) {
            candidateAttributes = attributes;
        }
    }

    return CGPointMake(candidateAttributes.center.x - halfWidth, offset.y);

}

参考链接:

http://stackoverflow.com/questions/20496850/uicollectionview-with-paging-setting-page-width?noredirect=1&lq=1

You can’t perform that action at this time.