UIPageViewControllerDatasource, causing both scrolling left and swipes right

If viewControllerBeforeViewController viewControllerAfterViewController, any of the methods returns nil, the counting part is also called.

Is this behavior expected? Anyway, I can stop it. This is what I am trying to do.

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    if ([viewController isKindOfClass:[QuestionViewController class]]){
        QuestionViewController* qvc = (QuestionViewController*)viewController;

        CVPQuestion* prevQuestion = [_surveyContext previousQuestion:qvc.question];
        return [self questionControllerWithQuestion:prevQuestion];
    }
    if ([viewController isKindOfClass:[SurveyEndedViewController class]]){
        return [self questionControllerWithQuestion:[self.questionList lastObject]];
    }
    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(QuestionViewController *)viewController {

    if ([viewController respondsToSelector:@selector(question)]){
        CVPQuestion* nextQuestion = [_surveyContext previousQuestion:viewController.question];
        if (nextQuestion == nil && ![_surveyContext isAnswered:viewController.question]){
            [viewController  showToasterWithText:@"Please answer the question to continue"];
        }
        QuestionViewController* qc = [self questionControllerWithQuestion:[_surveyContext nextQuestion:viewController.question]];

        if (!qc){return [self surveyEndedController:viewController.question];}
        return qc;
    }
    return nil;
}

If viewControllerAfterViewControllercalled after viewControllerBeforeViewController, a notification will be sent [viewController showToasterWithText:@"Please answer the question to continue"];.

+4
source share
1 answer

Looks like the "UIPageviewcontroller" doesn't actually use the "UIScrollViewDelegate". So subclassing "UIPageViewController" and using "UIScrollViewDelegate" seem to do the trick to learn how to scroll forward or backward.

// PageViewController scrollView

 - (void)viewDidLoad
     {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        for (UIView* v in [self.view subviews]){
            if ([v isKindOfClass:[UIScrollView class]]){
                _scrollView = (UIScrollView*)v;
            }
        }

        if (_scrollView){
            _scrollView.delegate = self;
        }
    }

// ,

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _startContentOffset = _scrollView.contentOffset;
    _transitionState = SurveyPageviewCtlrTransitionInitiated;
    [_transitionStateDelegate transitionInitiated:self];
}

//,

 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (_scrollView.contentOffset.x > _startContentOffset.x){
       [self.delegate forwardDrag];
     }
     else{
       [self.delegate reverseDrag];
 }
0

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


All Articles