6

I searched several stuffs on stackoverflow like

CGFloat pageWidth = scrollView.frame.size.width;
int pageNumberToBeScrolled = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if (pageNumberToBeScrolled != 1) {
    // Do what I want
}

in

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

But this works only if I scrolled slowly so scrolling page is definitely moved over half of it. This doesn't work if I scrolled fast like swipe. I just scrolled like 30% of scrolling page, which doesn't satisfy the situation over there, but it scrolled to next page, and I want to detect it.

How can I detect or catch all scroll-to-next-page situations? Please give me some help :)

EDIT

I working on this because I want to play a effect sound when scroll-to-next-page happens. I should detect right after my finger is off the screen and only for next page scrolling. I think if I handle this on scrollViewDidEndDragging, it'll be best for me :)

4

2 回答 2

23

I'm using this code to detect if currentPage is changed, it doesn't matter you scroll very fast or very slow.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        static NSInteger previousPage = 0;
        CGFloat pageWidth = scrollView.frame.size.width;
        float fractionalPage = scrollView.contentOffset.x / pageWidth;
        NSInteger page = lround(fractionalPage);
        NSLog(@"%d",page);
        if (previousPage != page) {
            previousPage = page;
            /* Page did change */
        }  
}
于 2013-04-25T15:39:45.153 回答
1

try like this may be it'l helps you

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
        float fractionalPage = scrollView.contentOffset.x / 320;
        NSInteger page1 = lround(fractionalPage);
        NSLog(@"%d",page1);
}
于 2013-04-25T14:52:25.680 回答