Tracking page number in UIScrollView

Discussion in 'iOS Programming' started by jeremyapp, Jul 9, 2009.

  1. jeremyapp macrumors newbie

    Joined:
    Apr 30, 2008
    #1
    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!
     
  2. jeremyapp thread starter macrumors newbie

    Joined:
    Apr 30, 2008
    #2
    Update: Never mind, I figured it out. For anyone who's interested, here's the code:

    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.
     

Share This Page