How do you check what page a UIScrollView is on?

Discussion in 'iOS Development' started by bamhm182, Jul 12, 2009.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I've looked all over with no luck. I've tried numerous "solutions" that other people have posted in various corners of the internet and came up empty handed.

    I have a UIScrollView that I have enabled paging in, each page has a different picture, I need to find out what page the user is looking at so that I can determine which picture is being viewed. My UIScrollView is 55x55 and the pictures are all 51x51. I have paging and the scroll view set up and they work just fine, but like I said, I need to know which page the user is viewing. The UIScrollView only goes vertical.

    Can anyone point me in the right direction on how to do this? Thanks.

    EDIT: If you need help determining the pages of the scroll views, I figured it out in post #3. Look over that for help.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Just get the offset and divide it by the page size.


    Edit: not everything is going to be done for you in the API. Sometimes you have to write your own code.
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Thanks skylar. It looks like I was on the right track than. However, it isn't working, I'm assuming I must have the if statements wrong. Would you mind looking at my page number code to see if I have it right?

    Code:
    Pages = scrollView.contentOffset.y / 1430;
    EDIT: Nevermind, stupid mistake on my part as usual.

    For anyone who needs this, you'll need your code like this:

    Code:
    PageNumber = ((scrollView.contentOffset.y / *ScrollViewHeight*) + 1);
    *ScrollViewHeight* is the height of the scroll view as it appears on the screen. If you have a Scroll View that is the entire size of the screen, your *Scroll View Height* will be 480. It is not the entire length of the scroll view like I had originally thought.

    EDIT 3: As gojohnnyboi pointed out, I forgot to mention that my scroll view goes up and down, not left and right. You'll want the code above if your Scroll View goes up and down, and the code below if your scroll view goes left and right.

    Code:
    PageNumber = ((scrollView.contentOffset.x / *ScrollViewWidth*) + 1);
    *ScrollViewWidth* is the width of the scroll view as it appears on the screen. If you have a Scroll View that is the entire size of the screen, your *ScrollViewWidth* will be 320. It is not the entire length of the scroll view like I had originally thought.

    The + 1 isn't needed, but it just makes things a bit neater. Without it, the first page is 0, the second page is 1, etc. With it, the first page is 1, the second page is 2, etc.

    EDIT 2: Figured that I might as well post how to define actions for page numbers.

    Code:
    	if(Page == 1)
    	{
    		//Page 1 actions
    	}
    	if(Page == 2)
    	{
    		//Page 2 actions
    	}
    Page is defined as an integer in the header.

    EDIT 4: Example project.
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Is "Pages" an int?
    What error is it giving?
  5. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    I REALLY hope you made four of the same typos, and that you understand that the y coordinate is how far down something is, not how far over.
  6. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    No, that is correct. My scroll view is going down, not over. I'll point that out in the post above. Of course I realize that y is up/down and x is left/right. I might be a little absent minded at times, but that is just common knowledge.

    @Stephen.4, Pages was always an int. The problem was that it would always do the last action no matter what the if statements returned. The mistake I was making is that my equation gave back different answers than what I was looking for. I.E., my if statements were checking for 1, 2, 3, 4, 5, etc. but the equation was returning decimals. This was happening because I was putting 1430(the entire length of the scroll view) instead of the height of the scroll view as it appeared on the screen.

    EDIT: I made an example project with both a horizontal and a vertical scroll view. You press a button, and it checks for the page number. http://www.[PIRACY.mf].com/?hckfzzoehdz

Share This Page