I have a UIView with content and I am implementing a pull-to-refresh feature. How can I configure the UIScrollView to always bounce back to contentOffset of 0,0 (and make it look natural)?
I’ve seen plenty of examples of doing this with UITable View and have the pull to refresh behavior pretty down. My issue seems to be having a large content size to accomodate the hidden Pull-To-Refresh banner. If I scroll the content down, it will snap back up. If I pull the content up, it lazily scrolls back down but not to the same exact position.
I’ve implemented the below function, which will detect if the scroll view is scrolling. It will then reposition the view back to 0,0. This kind of works but looks jumpy or have lag in the animation.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
if(moveScrollView)
[moveScrollView invalidate];
moveScrollView = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveScrollViewToOrigin) userInfo:nil repeats:NO];
}
- (void)moveScrollViewToOrigin;
{
if(scrollView.contentOffset.y == 0)
return;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
[[self scrollView] setContentOffset:CGPointZero];
[UIView commitAnimations];
}
I’ve created a sample project with the problem and my solution here.
The scrolling position seems to behave as expected when pulling content down. It’s when the screen initially loads, the pull-to-refresh banner is hidden on top, and you push content up. The content doesn’t always come back down as far as it should. Sometimes the offset of y is 40 when it’s finished bouncing. This is all verifiable by my sample code I posted.
Since the problem is isolated to allowing contentOffset.y to be greater than zero I decided to disable that behavior. I placed the following in (void)scrollViewDidScroll:(UIScrollView *)scrollingView;