Question

I need to limit the bouncing in a UIScrollView so that it bounces as usual at the bottom but doesn't go further than X pixels at the top (the bottom doesn't matters).

Is there any way to restrict the bouncing size? I have think that maybe a method in the delegate such us scrollViewWillScroll (instead of scrollViewDidScroll) would allow me to consume those scroll events that move further than top+X but I have been unable to find a suitable one so far.

Any clues?

Était-ce utile?

La solution

scrollViewDidScroll: is the correct method for this. Simple adjust the contentOffset in there.

This example will restrict the top bounce to 20 pixels:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < -20) {
        scrollView.contentOffset = CGPointMake(0, -20);
    }
}

Note that there is a bit of an unnatural delay until the view is scrolled back to 0,0 when the reason for the bounce was a decelerated swipe, and not a drag. But I think there is no way to prevent this. Basically the scrollView still bounces the full way but it doesn't display it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top