iosiphonepaginationuiscrollview

Calculate position of UIScrollView


I'm working on a simple PDF reader, on the bottom I have a UIScrollView with all the pages, in the middle I have a label which should show the page numbers while you're scrolling thru.

But I can't figure out how to calculate the exact position and see what page is in the middle. I also have 2 types of PDFs, once is a double paged PDF and the other one is just a single side.

This is the way I'm drawing them in the scrollview

NSInteger xPos = 20;
for(int i = 0; i < [pdf numberOfPages]; i++)
{
    ThumbViewController *tmpThumb = [[ThumbViewController alloc] initWithNibName:nil bundle:nil page:[NSString stringWithFormat:@"%i", i + 1] andPDF:pdf];

    // SINGLE SIDE PDF
    if([_issue.presentation isEqualToString:@"1"])
    {
        tmpThumb.view.frame = CGRectMake(xPos, 5, 150, 200);

        xPos += 200;
    }
    // DOUBLE SIDE PDF
    else
    {
        tmpThumb.view.frame = CGRectMake(xPos, 5, 150, 200);

        // Draw double pages
        if((i % 2) == 1)
        {
            xPos += 105;
        }
        else
        {
            xPos += 140;
        }
    }

    [_scrollView addSubview:tmpThumb.view];

    [tmpThumb release];
}

Which gives me these nice thumbnails: enter image description here enter image description here

But I'm stuck on the next part, when scrolling thru the UIScrollView I can't find any good way on how to calcule which page is in the middle of the view, currently I tried this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Calculate what page is in the middle
    NSInteger contentSize = scrollView.contentSize.width / [_readerModel.pdf numberOfPages];
    NSInteger pageNumber = floor(scrollView.contentOffset.x / contentSize);

    // Single side
    if([_issue.presentation isEqualToString:@"1"]) pageNumber = floor(scrollView.contentOffset.x / contentSize);

    NSLog(@"%d / %d", contentSize, pageNumber);

    _thumbnailPopup.titleLabel.text = [_readerModel getTitleForPage:pageNumber inIssue:_readerModel.currentIssue];
    _thumbnailPopup.pageLabel.text = [NSString stringWithFormat:@"%d", pageNumber];
}

As you can see from the picture, the pages don't really match up (they accidentally do in the 2nd pic, but when you scroll further they don't) Does anyone have any experience with something like this?


Solution

  • Just to get back at this, I finally found a decent solution.

    When creating the thumbnails I saved all the positions in an array, when scrolling I just check if the position matches.

    I know this is not ideal, but calculating the position on the fly was a little hard because I have different sizes of thumbnails.

    NSInteger scrollPos = scrollView.contentOffset.x + (scrollView.frame.size.width / 2);
    
    for(NSDictionary *tmpDic in _thumbsPos) {
        NSInteger startPos = [[tmpDic objectForKey:@"start"] intValue];
        NSInteger endPos = [[tmpDic objectForKey:@"end"] intValue];
        NSInteger page = [[tmpDic objectForKey:@"page"] intValue];
    
        if(scrollPos >= startPos && scrollPos <= endPos) {
            _thumbnailPopup.titleLabel.text = [_readerModel getTitleForPage:page inIssue:_readerModel.currentIssue];
            _thumbnailPopup.pageLabel.text = [NSString stringWithFormat:@"%d", (page + 1)];
    
            return;
        }
    }