4

I'm new to XCode so I need your help please. I use a UIPageControl to show which cell of my collection View is now visible.

The problem is to get the visible cell:

I think in this Method

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

I should update the current Page. But how do I get the current Page?

Thanks for your help! :-)

Daniel Kappacher
  • 472
  • 1
  • 6
  • 15

2 Answers2

11

The page can be defined as the ratio between the offset and the size.

- (NSInteger)horizontalPageNumber:(UIScrollView *)scrollView {
    CGPoint contentOffset = scrollView.contentOffset;
    CGSize viewSize = scrollView.bounds.size;

    NSInteger horizontalPage = MAX(0.0, contentOffset.x / viewSize.width);

    // Here's how vertical would work...
    //NSInteger verticalPage = MAX(0.0, contentOffset.y / viewSize.height);

    return horizontalPage;
}

There are a couple ways to trigger this. You can do the work on every scrollViewDidScroll, but that's a bit excessive. A better way is to run it either when dragging is complete and there will be no further deceleration, or when deceleration ends, as follows:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.page.currentPage = [self horizontalPageNumber:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) self.page.currentPage = [self horizontalPageNumber:scrollView];
}
danh
  • 62,181
  • 10
  • 95
  • 136
  • thanks very much for your reply. It works when I move forward, but when I'm moving one cell back again it doesnt change back. I used the horizontal Page. What am i doing wrong? – Daniel Kappacher Apr 02 '13 at 15:21
  • @DanielKappacher, my bad - page is a function of the offset vs. the bounds. don't know what I was thinking, i've only written this method about 100 times. Fixed now. Sorry. – danh Apr 02 '13 at 15:35
  • Thanks for your answer. Now when I give the page (horizontalPage) in a NSLog it gives me always the right number when I scroll. But the PageControl only changes when I scroll forward. I have " [self.vimpPageControl setCurrentPage:page];" to set the currentPage. Did I forgot anything? – Daniel Kappacher Apr 02 '13 at 15:45
  • [self.vimpPageControl setCurrentPage:page]; Do i have to put this method also in the scrollViewDidEndDeceleating method? Thanks! – Daniel Kappacher Apr 02 '13 at 16:00
  • 1
    @DanielKappacher - couple things... 1) I'll update my answer to be zero based (adding the one is better for showing the user the page number as an integer), 2) there are a couple ways to trigger this. will also illustrate in my answer. – danh Apr 02 '13 at 16:05
0

If you use the pager, than is is the code should help you:

- (IBAction)pagerValueChanged 
{
    NSData* imgData = [images objectAtIndex:pager.currentPage];
    UIImage* img = [[UIImage alloc]initWithData:imgData];
    imageView.image = img;

    //NSLog(@"img width: %f, img height: %f", img.size.width, img.size.height);

    [img release];
}