简体   繁体   English

在iOS中限制UIScrollview的弹跳

[英]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). 我需要限制UIScrollView的弹跳,以便它像往常一样在底部弹跳,但不会超过顶部的X像素(底部无关紧要)。

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. 我认为也许委托中的一个方法,例如我们scrollViewWillScroll (而不是scrollViewDidScroll )将允许我使用那些比top + X更远的滚动事件,但到目前为止我一直找不到合适的滚动事件。

Any clues? 有线索吗?

scrollViewDidScroll: is the correct method for this. scrollViewDidScroll:是正确的方法。 Simple adjust the contentOffset in there. 在那里简单调整contentOffset。

This example will restrict the top bounce to 20 pixels: 此示例将顶部反弹限制为20像素:

- (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. 请注意,当视图滚动回到0,0时,有一点不自然的延迟,此时反弹的原因是减速滑动,而不是拖动。 But I think there is no way to prevent this. 但我认为没有办法阻止这种情况。 Basically the scrollView still bounces the full way but it doesn't display it. 基本上,scrollView仍然以完整的方式反弹,但它不会显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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