How to make a UIScrollView snap to icons (like App Store: Feature)

后端 未结 4 1682
渐次进展
渐次进展 2020-12-24 10:14

What I want to get is the same behaviour that this scroll view has:

\"App

4条回答
  •  太阳男子
    2020-12-24 10:32

    Setting scrollView.decelerationRate = UIScrollViewDecelerationRateFast, combined with implementing scrollViewWillEndDragging:withVelocity:targetContentOffset:, seems to work for me using a collection view.

    First, I give myself some instance variables:

    @implementation ViewController {
        NSString *cellClassName;
        CGFloat baseOffset;
        CGFloat offsetStep;
    }
    

    In viewDidLoad, I set the view's decelerationRate:

    - (void)viewDidLoad {
        [super viewDidLoad];
        cellClassName = NSStringFromClass([MyCell class]);
        [self.collectionView registerNib:[UINib nibWithNibName:cellClassName bundle:nil] forCellWithReuseIdentifier:cellClassName];
        self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
    }
    

    I need offsetStep to be the size of an integral number of items that fit in the view's on-screen bounds. I compute it in viewDidLayoutSubviews:

    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
        UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
        CGFloat stepUnit = layout.itemSize.width + layout.minimumLineSpacing;
        offsetStep = stepUnit * floorf(self.collectionView.bounds.size.width / stepUnit);
    }
    

    I need baseOffset to the be the X offset of the view before scrolling starts. I initialize it in viewDidAppear::

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        baseOffset = self.collectionView.contentOffset.x;
    }
    

    Then I need to force the view to scroll in steps of offsetStep. I do that in scrollViewWillEndDragging:withVelocity:targetContentOffset:. Depending on the velocity, I increase or decrease baseOffset by offsetStep. But I clamp baseOffset to a minimum of 0 and a maximum of the contentSize.width - bounds.size.width.

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
        if (velocity.x < 0) {
            baseOffset = MAX(0, baseOffset - offsetStep);
        } else if (velocity.x > 0) {
            baseOffset = MIN(scrollView.contentSize.width - scrollView.bounds.size.width, baseOffset + offsetStep);
        }
        targetContentOffset->x = baseOffset;
    }
    

    Note that I don't care what targetContentOffset->x comes in as.

    This has the effect of aligning to the left edge of the leftmost visible item, until the user scrolls all the way to the last item. At that point it aligns to the right edge of the rightmost visible item, until the user scroll all the way to the left. This seems to match the behavior of the App Store app.

    If that doesn't work for you, you can try replacing the last line (targetContentOffset->x = baseOffset) with this:

        dispatch_async(dispatch_get_main_queue(), ^{
            [scrollView setContentOffset:CGPointMake(baseOffset, 0) animated:YES];
        });
    

    That also works for me.

    You can find my test app in this git repository.

提交回复
热议问题