UIPageViewController,如何正确跳转到特定页面而不会弄乱数据源指定的顺序? [英] UIPageViewController, how do I correctly jump to a specific page without messing up the order specified by the data source?

查看:578
本文介绍了UIPageViewController,如何正确跳转到特定页面而不会弄乱数据源指定的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些关于如何让 UIPageViewController 跳转到特定页面的问题,但是我注意到跳过的一个问题是没有任何答案似乎承认。

I've found a few questions about how to make a UIPageViewController jump to a specific page, but I've noticed an added problem with jumping that none of the answers seem to acknowledge.

没有深入了解我的iOS应用程序的细节(类似于分页日历),这就是我遇到的情况。我声明了一个 UIPageViewController ,设置了当前的视图控制器,并实现了一个数据源。

Without going into the details of my iOS app (which is similar to a paged calendar), here is what I'm experiencing. I declare a UIPageViewController, set the current view controller, and implement a data source.

// end of the init method
        pageViewController = [[UIPageViewController alloc] 
        initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                        options:nil];
        pageViewController.dataSource = self;
        [self jumpToDay:0];
}

//...

- (void)jumpToDay:(NSInteger)day {
        UIViewController *controller = [self dequeuePreviousDayViewControllerWithDaysBack:day];
        [pageViewController setViewControllers:@[controller]
                                    direction:UIPageViewControllerNavigationDirectionForward
                                     animated:YES
                                   completion:nil];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
        NSInteger days = ((THDayViewController *)viewController).daysAgo;
        return [self dequeuePreviousDayViewControllerWithDaysBack:days + 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
        NSInteger days = ((THDayViewController *)viewController).daysAgo;
        return [self dequeuePreviousDayViewControllerWithDaysBack:days - 1];
}

- (UIViewController *)dequeuePreviousDayViewControllerWithDaysBack:(NSInteger)days {
        return [[THPreviousDayViewController alloc] initWithDaysAgo:days];
}

编辑注意:我为出列方法添加了简化代码。即使有这个亵渎性的实现,我也有与页面顺序完全相同的问题。

Edit Note: I added simplified code for the dequeuing method. Even with this blasphemous implementation I have the exact same problem with page order.

初始化全部按预期工作。增量分页也可以正常工作。问题是,如果我再次拨打 jumpToDay ,订单就会混乱。

The initialization all works as expected. The incremental paging all works fine as well. The issue is that if I ever call jumpToDay again, the order gets jumbled.

如果用户在某一天-5并跳到第1天,向左滚动将再次显示第-5天而不是适当的第0天。这似乎与 UIPageViewController 保持如何有关对附近页面的引用,但我找不到任何方法可以强制它刷新它的缓存。

If the user is on day -5 and jumps to day 1, a scroll to the left will reveal day -5 again instead of the appropriate day 0. This seems to have something to do with how UIPageViewController keeps references to nearby pages, but I can't find any reference to a method that would force it to refresh it's cache.

任何想法?

推荐答案

编程iOS6 ,按Matt Neuburg记录了这个确切的问题,我实际上发现他的解决方案感觉比目前接受的答案好一些。该解决方案效果很好,具有负面的副作用,即在之前/之后动画到图像,然后用所需的页面进行剧烈的替换。我觉得这是一种奇怪的用户体验,Matt的解决方案可以解决这个问题。

Programming iOS6, by Matt Neuburg documents this exact problem, and I actually found that his solution feels a little better than the currently accepted answer. That solution, which works great, has a negative side effect of animating to the image before/after, and then jarringly replacing that page with the desired page. I felt like that was a weird user experience, and Matt's solution takes care of that.

__weak UIPageViewController* pvcw = pvc;
[pvc setViewControllers:@[page]
              direction:UIPageViewControllerNavigationDirectionForward
               animated:YES completion:^(BOOL finished) {
                   UIPageViewController* pvcs = pvcw;
                   if (!pvcs) return;
                   dispatch_async(dispatch_get_main_queue(), ^{
                       [pvcs setViewControllers:@[page]
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO completion:nil];
                   });
               }];

这篇关于UIPageViewController,如何正确跳转到特定页面而不会弄乱数据源指定的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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