Jump to a specific/last page in a uiscrollview?

Discussion in 'iOS Programming' started by cstromme, Aug 18, 2009.

  1. cstromme macrumors regular

    Joined:
    Feb 26, 2007
    #1
    Have a UIScrollView setup here with multiple horizontal pages, and I'd like to add a button to move to the last page of the scrollview, as it might have over 1000 pages.

    I thought scrollRectToVisible:Animated: might be the way to go, but I added that to viewWillAppear and with the scrollviews bounds as parameter but it doesn't seem to do anything.

    Any ideas?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    1) Post your code.

    2) You scrolled to the bounds of the scroll view? I don't think that would work. You need to tell it the rect of the view being scrolled (the content view) that you want to be visible at the end of the scroll operation.
     
  3. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #3
    Here's my viewWillAppear:

    Code:
    - (void)viewWillAppear:(BOOL)animated {
        // view controllers are created lazily
        // in the meantime, load the array with placeholders which will be replaced on demand
        NSMutableArray *controllers = [[NSMutableArray alloc] init];
        for (unsigned i = 0; i < pages; i++) {
            [controllers addObject:[NSNull null]];
        }
        self.viewControllers = controllers;
        [controllers release];
    
        // a page is the width of the scroll view
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * pages, scrollView.frame.size.height);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.scrollsToTop = NO;
        scrollView.delegate = self;
    
        // pages are created on demand
        // load the visible page
        // load the page on either side to avoid flashes when the user starts scrolling
        [self loadScrollViewWithPage:0];
        [self loadScrollViewWithPage:1];
    	
    	CGRect rootViewBounds = self.parentViewController.view.bounds;
    	CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
    	CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
    	pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (rootViewHeight - 20), rootViewWidth, 20)];
    	[pageLabel setBackgroundColor:[UIColor blackColor]];
    	[pageLabel setTextColor:[UIColor whiteColor]];
    	[pageLabel setTextAlignment:UITextAlignmentCenter];
    	pageLabel.text = [[NSString stringWithFormat:@"1 of %i",pages] retain];
    	[self.navigationController.view addSubview:pageLabel];	
    //	[self.scrollView scrollRectToVisible:HELPZ animated:YES];
    }
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I'm assuming that the loadScrollViewWithPage: method creates a page, adds it to the content view of the scroll view and correctly updates the content view size etc?

    If so then I'd do something like this:

    Code:
    CGRect lastVisibleRect;
    CGSize contentSize = [self.scrollView contentSize];
    lastVisibleRect.size.height = contentSize.height; //We want the visible rect height to be the content view height as we are only scrolling horizontally
    lastVisibleRect.origin.y = 0.0; // So the y origin should be 0
    lastVisibleRect.size.width = rootViewWidth; // The visible rect width should be as wide as the screen
    lastVisibleRect.origin.x  = contentSize.width - rootViewWidth; // And the x position should be all the way to the right - one page width (assumes a page is as wide as the screen)
    [self.scrollView scrollRectToVisible:lastVisibleRect animated:NO]; // Why animate?  The view isn't visible yet!
    
    So what we do is calculate the rect of the content view we want to be visible and scroll to it. If this doesn't work you should be able to work out why and fix it.
     
  5. cstromme thread starter macrumors regular

    Joined:
    Feb 26, 2007
    #5
    That worked perfectly! I see now where I went wrong.
     

Share This Page