Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit bouncing for UIScrollview in iOS

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?

like image 273
HiddenDev Avatar asked Dec 12 '12 14:12

HiddenDev


1 Answers

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.

like image 125
Matthias Bauch Avatar answered Oct 14 '22 16:10

Matthias Bauch