UIPageViewController 以编程方式进行中间转换?

UIPageViewController intermediate transitions programmatically?

我有一个 UIPageViewController 有 n (n>>2) 页。第一页是黄色的,第二页是蓝色的。当我从一个页面拖到另一个页面时,我得到了一个很好的中间状态动画,如下图所示:

我试图以编程方式实现从一个页面到另一个页面的页面转换。我得到 UIScrollView 作为:

for view:UIView in self.view.subviews as! [UIView] {
  if view.isKindOfClass(UIScrollView.self) {
    self.pageScrollView = view as? UIScrollView
  }
}

我这样设置滚动位置:

self.pageScrollView!.contentOffset.x = screenWidth - deltaX

结果如下:

滚动位置已更新,但未加载下一个视图。这就是为什么在正确的站点上有一个白色区域。我知道我可以用 setViewControllers 设置下一页,但这会显示整个下一页。我在这里想要的是以编程方式仅部分显示下一页,就像我在拖动时所做的那样。

我知道这是可以做到的,因为 Tinder 应用程序已经在这样做了。当您在 header 上做出平移手势时,页面视图控制器会非常流畅地切换页面。

如何以编程方式模拟页面中间转换?

您不能在已分页 的内容上中途停止。只要 drag 手势处于活动状态就可以,但是一旦您抬起手指并且该手势停止,控件将显示下一个 UIViewController 或已经存在的 UIViewController呈现。

setViewControllersanimated:true 一起使用将从当前的 UIViewController 平滑过渡到下一个 UIViewController,就好像您用手指 拖动 视图一样:

.setViewControllers([allViewControllers[1]], direction: .Forward, animated: true, completion: nil)

如果您想要更自定义的内容,您可能需要使用 UIScrollView 而不是 UIPageViewController,并自行设置内容。将 pagingEnabled 设置为 true 将使其表现得像具有 Scroll 转换的 UIPageViewController,但您也可以禁用分页并使用 setContentOffset:(contentOffset, animated) 的组合来实现它滚动到您喜欢的位置,委托 scrollViewWillEndDragging:(scrollView, withVelocity, targetContentOffset) 使其成为 "page".


我创建了一个 code sample here 来说明我的意思。它是一个模仿 UIPageViewController 的简单 UIScrollView,具有基本视图 allocation/deallocation,以及一种以编程方式中途滚动的方法。

我遇到了同样的问题,并通过设置一个 viewController 来进行转换来修复它。

喜欢objective-c

[self.pageControl setViewControllers:@[[self.viewControllers objectAtIndex:selectedPageIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {

    }];

基本上在swift你需要写的是:

pageViewController.setViewControllers([yourVC()], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

这里有一个很好的教程:

https://www.youtube.com/watch?v=8bltsDG2ENQ