devinross
devinross

Reputation: 2816

UIScrollView - Figuring out where the scroll will stop

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;

Upvotes: 3

Views: 1245

Answers (5)

Jason Crocker
Jason Crocker

Reputation: 541

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.

Upvotes: 3

devinross
devinross

Reputation: 2816

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.

Upvotes: 0

Ben Zotto
Ben Zotto

Reputation: 71058

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

Upvotes: 2

Jens Utbult
Jens Utbult

Reputation: 923

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.

Upvotes: 0

Jaanus
Jaanus

Reputation: 17866

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?

Upvotes: 0

Related Questions