UIPageViewControllerDatasource calling both swipe left and swipe right methods

拥有回忆 提交于 2019-12-07 11:11:10

问题


If viewControllerBeforeViewController viewControllerAfterViewControllereither of the method returns nil the counter part is also called.

Is this behaviour expected behaviour ? Any way i can stop this from happening. This is what I am tried 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 viewControllerAfterViewController will get called after viewControllerBeforeViewController the notification [viewController showToasterWithText:@"Please answer the question to continue"];will be sent too.


回答1:


Looks like 'UIPageviewcontroller' is not really using the 'UIScrollViewDelegate'. So subclassing the 'UIPageViewController' and using the 'UIScrollViewDelegate' seems to do the trick to find out wether its forward swipe or backward swipe.

//Get Scroll View from PageViewController the 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;
        }
    }

//Record content offset when scroll view begins dragging

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

// Direction based on displacement

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


来源:https://stackoverflow.com/questions/23286757/uipageviewcontrollerdatasource-calling-both-swipe-left-and-swipe-right-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!