UIPageViewControllerDatasource调用向左滑动和向右滑动方法 [英] UIPageViewControllerDatasource calling both swipe left and swipe right methods

查看:220
本文介绍了UIPageViewControllerDatasource调用向左滑动和向右滑动方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

如果 viewControllerAfterViewController 将在 viewControllerBeforeViewController之后调用通知 [viewController showToasterWithText:@请回答问题继续]; 将被发送

推荐答案

看起来'UIPageviewcontroller'并没有真正使用'UIScrollViewDelegate'。所以继承'UIPageViewController'并使用'UIScrollViewDelegate'似乎可以找到它向前滑动或向后滑动的技巧。

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.

//从PageViewController获取滚动视图scrollView

//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];
}

//基于排量的方向

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

这篇关于UIPageViewControllerDatasource调用向左滑动和向右滑动方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆