pixelfreak
pixelfreak

Reputation: 17844

Is it possible to know the final UIScrollView contentOffset before it ends decelerating?

When user "flicks" a UIScrollView, causing it to scroll with momentum, is there a way to figure out the final contentOffset before the deceleration ends?

Basically, I would like to know what the ultimate contentOffset is from inside scrollViewDidEndDragging:willDecelerate: instead of scrollViewDidEndDecelerating:

There is a float property called decelerationRate, which might be one piece of the puzzle, but I have yet to figure out what to do with it.

PS: I have pagingEnabled set to YES. In iOS 5, there is actually scrollViewWillEndDragging:withVelocity:targetContentOffset:, but the doc says it's not fired if pagingEnabled is YES

Upvotes: 4

Views: 2823

Answers (2)

soryngod
soryngod

Reputation: 1827

As I noticed the max contentOffset can be calculated from the start it's just a difference between the scrollview contentSize and scrollview frame size. I calculated it on y like this. maxOffsetY = scrollView.contentSize.height - scrollview.frame.size.height;

Upvotes: 9

RBI
RBI

Reputation: 833

There might be a better way but off the top of my head I would try this:

  1. implement the scrollViewDidScroll: method of the delegate and get the contentOffsets of the first two times this method is called (of course they will have to be stored outside the method).

  2. get the difference between the two readings of the content offset; this will be your delta.

  3. now you can use the delta as the current velocity of your scroll and your decelerationRate to figure how much it will scroll before the delta becomes 0. this value +- (depending on scroll diection) the second reading of the content offset will be your "destination scroll offset".

be sure that you check that the scrollview is in fact decelerating before you apply this and also check to make sure your final calculation is not "out of bounds" in terms of the content size.

hope this helps

Upvotes: 2

Related Questions