Detecting UIScrollView Position

Here a solution for getting the current position (page) as integer Value: Use scrollViewDidEndDecelerating: methode:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"%i", (int)(_scrollView.contentOffset.x / _scrollView.frame.size.width));
}

I think this could help you :). With scrollViewDidScroll you always get the exact position of your scrollView:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < 320)
    {
         // Load content 1
    }
    else if (scrollView.contentOffset.x >= 320)
    {
         // Load content 2
    }
}

NSLog(@"Position: %g", scrollView.contentOffset.x);

Do not forget the delegate UIScrollViewDelegate in your .h file.


It's hard to say because you never specifically mentioned what the problem is, but I'll take a couple guesses at it.

If the delegate method isn't being called at all you need to remember to set the scroll views delegate:

[myScrollView setDelegate:self];

Otherwise, the problem with using scrollViewDidEndDragging to detect the scroll views offset is that it will check the offset at the point where you stop dragging which could be within the destination pages rect, or within the starting pages rect. As an alternative, I'd suggest you use scrollViewDidEndDecelerating to check the content offset as it will be called as soon as the scroll view comes to a stop at its destination page.

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x < 320) {
        NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
    }
    else if (scrollView.contentOffset.x >= 320) {
        NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
    }
}