简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM