在scrollViewDidScroll中更改UIScrollView框架可以删除顶部的弹跳

时间:2015-01-22 19:19:08

标签: ios objective-c uiscrollview bounce

发生了一些奇怪的事情,当我在UIScrollView中滚动时,我想将它向上移动并扩展它的高度,因为它默认太小了。它运行正常,我听了scrollViewDidScroll委托但是禁用了顶部的弹跳!

即使我在scrollViewDidScroll中执行此操作,顶部弹跳也消失了,底部仍然反弹:

scrollView.frame = CGRectOffset(self.scrollView.frame, 0, 0);

这是我在scrollViewDidScroll中的实际代码:

self.maxScrollDistance = 400;

if(scrollView.contentOffset.y <= self.maxScrollDistance) {
    float newY = self.startScrollViewY - scrollView.contentOffset.y / 3;
    float newScrollViewHeight = self.view.frame.size.height - newY;
    scrollView.frame = CGRectMake(0, newY, self.scrollView.frame.size.width, newScrollViewHeight);
}

任何提示可能出现问题的人,一个关于问题的视频: Bounce Problem Simulator recording

谢谢你们!

1 个答案:

答案 0 :(得分:0)

滚动到底部时,您的框架总是在变化,并且它不会弹跳,因为在负内容中滚动时更改框架OffOffset.y

if(scrollView.contentOffset.y <= self.maxScrollDistance)

尝试捕获scrollView.contentOffset.y&lt; 0并设置其更改框架。

if(scrollView.contentOffset.y < self.maxScrollDistance && scrollView.contentOffset.y>0) {
    // Todo
}else if(scrollView.contentOffset.y <= -self.maxScrollDistance && scrollView.contentOffset.y<0){
    // Todo
}

//试试这段代码:

if(scrollView.contentOffset.y < self.maxScrollDistance && scrollView.contentOffset.y>0) {
    float newY = self.startScrollViewY - scrollView.contentOffset.y / 3;
    float newScrollViewHeight = self.view.frame.size.height - newY;
    scrollView.frame = CGRectMake(0, newY, self.scrollView.frame.size.width, newScrollViewHeight);

}else if(scrollView.contentOffset.y <= -self.maxScrollDistance && scrollView.contentOffset.y<0){
    float newY = self.startScrollViewY + scrollView.contentOffset.y / 3;
    float newScrollViewHeight = self.view.frame.size.height + newY;
    scrollView.frame = CGRectMake(0, newY, self.scrollView.frame.size.width, newScrollViewHeight);
}