Iphone – UIScrollView determine scroll direction on scrollViewDidEndDragging when paging

iphoneuiscrollview

Does anyone know how to determine which direction a scroll view will move when the user lifts their finger, i.e. when scrollViewDidEndDragging gets called?

Specifically, when the scroll view is set to paging.

Most of the time, I can just track and check the contentOffset in scrollViewDidScroll, however, there are special cases where the user flicks the screen quickly. In some cases a LEFT-RIGHT-LEFT move will scroll to the next page, and in others, the same pattern will remain on the same page.

I'm guessing it has something to do the with acceleration (difference between the last few points) of the touch.

(I'm on iOS4.3 on an iPad)

Best Answer

You don't use timer, which is very expensive operation. Lock the direction of your scrollView first to move only up/down and left/right [I guess that's what you want]. And then use following code to determine...

CGPoint start, end;

- (void)scrollViewWillBeginDragging:(UIScrollView *)sender {
    start = self.scrollView.contentOffset;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)sender willDecelerate:(BOOL)decelerate {
    end = self.scrollView.contentOffset;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {    
    NSLog(@"%d %d",(int)start.x,(int)start.y);
    NSLog(@"%d %d",(int)end.x,(int)end.y);
}