objective-ccocoa-touchuiscrollviewuiscrollviewdelegate

Units / Dimensions for UIScrollView decelerationRate and velocity?


I am trying to get the velocity of a scrolling UIScrollView after the user has lifted his/her finger so that I can trigger an event when the scrollview's velocity drops below a threshold velocity.

The Apple documentation states that the units for a scrollview's velocity are in points and I would guess that would be per second (pts/s), e.g., for UIScrollView Delegate method - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset.

I would therefore assume that the units for a scrollview's deceleration would be points per second per second (pts/s^2) but this does not seem to be the case.

Here are some example parameters pulled from a scrolling scroll view as soon as the pan gesture event ends (i.e., as soon as you lift your finger) pulled from methods - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffsetand - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView:

(Initial offset, Target offset, Initial Velocity, Final Velocity, Deceleration, Elapsed Time) =

where:


Solution

  • The most important delegate method for your purposes is probably scrollViewDidScroll:, because you just keep getting sent that message over and over and over, including during the deceleration. Monitoring what's happening is going to be more use to you than trying to calculate in advance. There are no useful "units for a scroll view's deceleration" - that is to say, you don't have any information that could allow you to calculate in advance a velocity at every moment during the deceleration. However, scrollViewWillEndDragging:withVelocity:targetContentOffset: tells you the velocity now and the offset that the scroll view will have when the velocity is zero, and you can ask for the offset now, so you can decide on an offset between the current offset and the final offset, and monitor the remainder of the scrolling as it proceeds, in scrollViewDidScroll:, to learn when that offset is passed. And of course you can take a timestamp every time scrollViewDidScroll: is called, so with that and the instantaneous offset, along with the record from all the previous calls, presto, there's your instantaneous velocity.