How to determine the next page number of a UIScrollView, which is "scrollView.pagingEnabled" = YES? in 'scrollViewDidEndDragging'?

I was looking for a few things in stackoverflow for example

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 only works if I scroll slowly, so the page scroll definitely moves halfway. This does not work if I scroll quickly as I swipe my finger across the screen. I just scrolled like a 30% page scroll, which does not satisfy the situation there, but it scrolls to the next page, and I want to find it.

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

EDIT

I am working on this because I want to play a sound effect while scrolling to the next page. I have to detect right after my finger from the screen and only to scroll the next page. I think that if I handle this on scrollViewDidEndDragging , that would be better for me :)

+6
source share
2 answers

I use this code to determine if currentPage is changing, no matter what 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 */ } } 
+22
source

try this maybe it will help you

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { float fractionalPage = scrollView.contentOffset.x / 320; NSInteger page1 = lround(fractionalPage); NSLog(@"%d",page1); } 
+1
source

Source: https://habr.com/ru/post/943626/


All Articles