I have a horizontal scrollView . 0 is the start screen. I want to set restrictions for buttons. next should scroll to the right from 0 to 5. back should scroll to the left from 5 to 0 (not -1). How to do it?

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

    2 answers 2

    in next: finish with the first line

     if(self.currentPage == 5) return; // или 4, если у вас 5 экранов всего 

    in back:

     if(self.currentPage == 0) return; 

    to just ignore clicks.

    • Thank! It works. But there is a problem. If I scroll with the buttons, and then use gestures, the buttons stop working or behave strangely (scrolling forward on several screens or not scrolling at all) How can I synchronize gestures with buttons? - User
    • This is due to the fact that when you swipe your currentPage does not change. you can either disable userInteraction and force the user to use the buttons, or get rid of currentPage and calculate the offset dynamically - Max Mikheyenko

    Before scaling check scroll.contentOffset.x. It must not be less than 0 and no more pageWidth * 4.

    Ideally, after each shift you check scroll.contentOffset.x. If it == 0, then deactivate the back button. Similarly for the next button.