PDA

View Full Version : Tracking page number in UIScrollView




jeremyapp
Jul 9, 2009, 06:39 PM
Hey,

I've been having some trouble with a UIScrollView in my project. Is there a way to track which "page" a UIScrollView is on? I have tried using an integer "currentPage" and incrementing it with delegate methods, but I haven't found this to be accurate (and nor does it allow for me to track when the user scrolls backwards).

I haven't been able to find a good way to do it yet. Anyone have a good way to do this?

Thanks!



jeremyapp
Jul 9, 2009, 07:06 PM
Update: Never mind, I figured it out. For anyone who's interested, here's the code:


- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = scrollView.frame.size.width;
int currentPage = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}


If anyone has a better way, let me know.