-->

How to slow down scrolling when showing preview of

2019-05-11 06:35发布

问题:

I am aware that there're many questions was already asked for this issue. But most of the questions are outdated or with no answers. The problem with my implementation is not preview, but its pagination speed?

I am able to show previous/next cell in UICollectionView but when I try to scroll it speedily, its scrolling (by skipping) 1 or 2 pages. This happens when I scrolling it at good speed.

For a note, I'm using custom layout.

This is my code:

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

    CGRect cvBounds = self.collectionView.bounds;
    CGFloat halfWidth = cvBounds.size.width * 0.5f;
    CGFloat proposedContentOffsetCenterX = proposedContentOffset.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 (fabsf(attributes.center.x - proposedContentOffsetCenterX) <
            fabsf(candidateAttributes.center.x - proposedContentOffsetCenterX)) {
            candidateAttributes = attributes;
        }
    }

    return CGPointMake(round(candidateAttributes.center.x - halfWidth), proposedContentOffset.y);
}

Any help would be highly appreciated.