UIPageViewControllerDatasource 调用向左滑动和向右滑动方法

ila*_*ila 5 ios uipageviewcontroller

如果viewControllerBeforeViewController viewControllerAfterViewController任一方法返回 nil,则也会调用计数器部分。

这种行为是预期行为吗?无论如何我都可以阻止这件事发生。这就是我试图做的事情。

- (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;
}
Run Code Online (Sandbox Code Playgroud)

如果viewControllerAfterViewController也会在viewControllerBeforeViewController通知[viewController showToasterWithText:@"Please answer the question to continue"];发送后接到电话。

ila*_*ila 0

看起来“UIPageviewcontroller”并没有真正使用“UIScrollViewDelegate”。因此,子类化“UIPageViewController”并使用“UIScrollViewDelegate”似乎可以解决它是向前滑动还是向后滑动的问题。

//从PageViewController中获取ScrollView的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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

//记录滚动视图开始拖动时的内容偏移量

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

// 基于位移的方向

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