How can I programmatically force-stop scrolling in a UIScrollView?
I played with your original solution a bit, and this seems to work just fine. I think you almost had it, but you were just offsetting the rect that you used too much, and forgot that you could just scroll the rect straight back to the original rect.
The generalized solution for any scrolling action is this:
- (void)killScroll
{
CGPoint offset = scrollView.contentOffset;
offset.x -= 1.0;
offset.y -= 1.0;
[scrollView setContentOffset:offset animated:NO];
offset.x += 1.0;
offset.y += 1.0;
[scrollView setContentOffset:offset animated:NO];
}
[Edit] As of iOS 4.3 (and possibly earlier) this also appears to work
- (void)killScroll
{
CGPoint offset = scrollView.contentOffset;
[scrollView setContentOffset:offset animated:NO];
}
The generic answer is, that [scrollView setContentOffset:offset animated:NO]
is not the same as [scrollView setContentOffset:offset]
!
[scrollView setContentOffset:offset animated:NO]
actually stops any running animation.[scrollView setContentOffset:offset]
doesn't stop any running animation.- Same for
scrollView.contentOffset = offset
: doesn't stop any running animation.
That's not documented anywhere, but that's the behavior as tested on iOS 6.1 & iOS 7.1 - probably also before.
So the solution to stop a running animation / deceleration is simple as that:
[scrollView setContentOffset:scrollView.contentOffset animated:NO];
Basically what David Liu said in his edited answer. But I wanted to make clear, that these two APIs are NOT the same.
Swift 3:
scrollView.setContentOffset(scrollView.contentOffset, animated:false)
For me, David Lui's accepted answer above didn't work for me. This is what I ended up doing:
- (void)killScroll {
self.scrollView.scrollEnabled = NO;
self.scrollView.scrollEnabled = YES;
}
For what it is worth, I'm using the iOS 6.0 iPhone Simulator.