Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect next page number of UIScrollView which is 'scrollView.pagingEnabled' = YES? in 'scrollViewDidEndDragging'?

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 :)

like image 941
Wooseong Kim Avatar asked Apr 25 '13 14:04

Wooseong Kim


2 Answers

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 */
        }  
}
like image 78
limon Avatar answered Nov 06 '22 23:11

limon


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);
}
like image 21
Balu Avatar answered Nov 07 '22 00:11

Balu