I have a scrollView with 6 screens. I use it with paging mode. When I click on the nextButton go to the next screen. I click on the backButton switch to the previous screen. Everything works fine if I start scrolling from the first screen. But if I click on nextButton and start scrolling from the 5th screen, I turn to the 2nd screen. How to scroll normally from each screen?

nextButton

 -(IBAction)next:(id)sender { if(self.currentPage == 6) return; self.currentPage++; CGFloat pageWidth = _scroller.frame.size.width; [_scroller setContentOffset:CGPointMake(pageWidth*self.currentPage, 0) animated:YES]; } 

backButton

 -(IBAction)back:(id)sender { if(self.currentPage == 0) return; self.currentPage--; CGFloat pageWidth = _scroller.frame.size.width; [_scroller setContentOffset:CGPointMake(pageWidth*self.currentPage, 0) animated:YES]; } 
  • when you scroll through self.currentPage does not change, so when you click next, it jumps to currentPage. you need to change currentPage in delegate methods - Max Mikheyenko

1 answer 1

Use this UIScrollView delegate method.

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if ([scrollView isEqual:_scroller]) { CGFloat pageWidth = CGRectGetWidth(self.bounds); CGFloat pageFraction = self.scrollImage.contentOffset.x / pageWidth; NSLog(@"%d", (NSInteger)pageFraction); } } 

In NSLog numeric parameter of the scroll you selected is displayed, when you press the button, you need to multiply your width by this chil parameter + 1 (this is if you scroll right, if you need to scroll left, change the parameter to -1)