Reputation: 17844
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
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
Reputation: 833
There might be a better way but off the top of my head I would try this:
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).
get the difference between the two readings of the content offset; this will be your delta.
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