How to use drag and drop within a UIScrollView
Posted
by user249488
on Stack Overflow
See other posts from Stack Overflow
or by user249488
Published on 2010-03-21T03:29:54Z
Indexed on
2010/03/21
3:31 UTC
Read the original article
Hit count: 639
I am trying to drag and drop UIViews within a UIScrollView. I'd like the UIScrollView to scroll if I drag a UIView so that it intersects the top or bottom of the ScrollView. If it touches the top, it should scroll up. If it touches the bottom, it should scroll down. Once the UIView is moved such that it is no longer intersecting the top or bottom, the scrolling should stop.
The first approach I tried was to start an NSTimer whenever I detected that the UIView intersected with the top or bottom of the scrollview. The timer would scroll the UIScrollView bit by bit until it was invalidated. This resulted in very jerky scrolling, since there is no way to predict when the timers will fire.
My current approach is to simply scroll all the way to the top or bottom of the ScrollView, like this:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[self.parentScrollview setContentOffset:newOffset animated:NO];
[UIView commitAnimations];
In order to stop scrolling, I try to determine the position of the touch in the touchesMoved: delegate, and use that to calculate the contentOffset to use for the scrollView.
The problem is, when I try to get the location in the view via [[touches anyObject] locationInView:self], I get the final end location, rather than the current location. I'm guessing that's because the contentOffset on the scrollView is set immediately.
Is there a way to get the actual location of a touch in a situation like this? Or any other way to set the contentOffset when I interrupt the scrolling animation?
© Stack Overflow or respective owner