Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the destination contentOffset of a UIScrollView in motion

I'm using a UIScrollView as the basis of a component that makes use of core animation. When the user swipes the view I would like to position elements according to the destination (resting) position of the scroll view. For this, I need to calculate the destination contentOffset of the UIScrollView in the scrollViewWillBeginDecelerating: method or similar.

The reason I need this is that I'll be using the destination contentOffset to animate views nested within the scrollview's contentView to their final position. I could of course set up an observer on the contentOffset or similar, but this would result in chaotic animation as the nested views would then update their positions multiple times during deceleration. I'd like this to happen just the once.

Is there a simple way to do this?

like image 637
Tricky Avatar asked Feb 07 '11 16:02

Tricky


People also ask

What is Contentoffset UIScrollView?

The point at which the origin of the content view is offset from the origin of the scroll view.

How UIScrollView works?

A floating-point value that determines the rate of deceleration after the user lifts their finger. We can assume that this rate indicates how much the scroll velocity will change after one millisecond (all UIScrollView values are expressed in milliseconds, unlike UIPanGestureRecognizer ).


1 Answers

There's a delegate method for this:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS5(5_0);

According to the doc:

Called on finger up if the user dragged. velocity is in points/second. targetContentOffset may be changed to adjust where the scroll view comes to rest. not called when pagingEnabled is YES

like image 165
Doc Avatar answered Oct 13 '22 13:10

Doc