Question

I'm trying to figure out how to calculate where the scrollview will stop when a user does a swipe gesture and the scrollview goes into deceleration. I'm trying to use the delegate functions, but I can't accurately figure it out. Please help!

- (void) scrollViewDidScroll:(UIScrollView *)scrollView;
- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
Was it helpful?

Solution 5

There is a variable for the current acceleration that is kept in the UIScrollView implementation but using that as a solution is impossible because you can't compile the code for a device.

OTHER TIPS

This thread is a bit old, but still comes up top in a search for this problem.

The UIScrollViewDelegate protocol contains the following method that tells your code where the scroll view is expected to stop...

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

The targetContentOffset parameter is an inout so if you set it to a different value, the scrollview will actually come to a stop at the offset you specify.

Unfortunately UIScrollView doesn't work this way-- there's no way to ask it up front where the deceleration ends.

Sounds complicated. The user may start scroll again while it is decelerating, stop it suddenly or what not. Maybe you can get by with just doing DidEndDecelerating, i.e just detecting the position when the scrolling is over?

It should be simple math. The contentOffset property of the scrollview is updated each time scrollViewDidScroll is called. You only need to save the vector between two positions and the time to get at least a descent end-position of the deceleration.

Note that the user can stop the deceleration any time by tapping on the scrollview as Jaanus pointed out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top