0

I want to use a UISlider control to manage UIPageViewController pagination. I have managed getting the number of pages, and those are the values that the slider uses to scroll the pagination.

Now, it is working. The problem comes when I change the value so quickly. Anyone knows how can I solve this point?

- (void)changeValueSlider:(UISlider *)slider {
   [self goToPageAtIndex:slider.value];
}

- (void)goToPageAtIndex:(NSUInteger)index {
    MNBBasePreviewViewController *currentViewController = self.pageViewController.viewControllers.firstObject;
    if (index < self.previewContainerInteractor.count){
        if (index < currentViewController.index) {
            [self backToPageAtIndex:index];
        } else if (index > currentViewController.index) {
            [self forwardToPageAtIndex:index];
        }
    }
}

- (void)forwardToPageAtIndex:(NSUInteger)index {
    MNBBasePreviewViewController *previewViewController = [self viewControllerAtIndex:index];
    [self.pageViewController setViewControllers:@[previewViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
}

- (void)backToPageAtIndex:(NSUInteger)index {
    MNBBasePreviewViewController *previewViewController = [self viewControllerAtIndex:index];
    [self.pageViewController setViewControllers:@[previewViewController] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL];
}
croigsalvador
  • 1,993
  • 2
  • 24
  • 47

1 Answers1

2

You can either update pageControl only when slider ended sliding by applying one of the techniques from the following post: iPhone : How to detect the end of slider drag?

Changing value continously while sliding is just too risky for this kind of operation. If you really for some reason require to update pages continously, you will have to come up with some compromise, and it will require some effort. On the first look I came up with this strategy:

  1. add some BOOL property, let's call it 'isPresenting' and set it to NO initially.
  2. on sliderValueChange, if isPresenting is NO, start presenting view.
  3. set property 'isPresenting' to YES.
  4. when each of the views finishes presenting itself, set isPresenting to NO.
  5. check if slider is sliding, if not call changeValueSlider: manually

Note: steps 4. and 5. should be executed from child views (viewDidAppear:), so you should use either protocol or local notification to do that, or have a reference to parent view controller in children and call parent's method from child.

Community
  • 1
  • 1
Frane Poljak
  • 2,315
  • 23
  • 25
  • I do almost the same. The problem comes when I call this method ` [self.pageViewController setViewControllers:@[previewViewController] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL]; } ` many times in a short time, it gets crazy for a moment – croigsalvador Mar 08 '16 at 15:16
  • That is the point of my answer, you should prevent it from being called too frequently, that's what the 'isPresenting' propery is for. You set the isPresenting to NO in the previewViewController in viewDidAppear, and start new transition only if isPresenting is NO – Frane Poljak Mar 08 '16 at 16:50
  • mmm, Ok I'll try it. Sorry, I didn't understand it – croigsalvador Mar 08 '16 at 16:59