以编程方式平滑滚动 UIPageViewController
Posted
技术标签:
【中文标题】以编程方式平滑滚动 UIPageViewController【英文标题】:Smooth scroll UIPageViewController programmatically 【发布时间】:2015-09-22 23:28:18 【问题描述】:我的应用中有一个带有导航栏的 UIViewController,如下 gif 所示。 UIViewController 包含一个具有 4 个页面的 UIPageViewController。导航栏上的每个选项卡都代表 UIPageViewController 中的一个页面。
我正在尝试在这些 UIPageViewController 页面之间实现平滑滚动,即如果我当前位于第一个选项卡上并且单击导航栏上的最后一个选项卡,则 UIPageViewController 必须平滑滚动所有选项卡到最后一个选项卡。虽然这现在有效,但我对滚动性能不满意,因为它看起来很紧张。我正在使用
- setViewControllers:direction:animated:completion:
api 在页面之间滚动。
这就是我的代码的样子
- (void)navigateToNextPage
//you have navigated to the destination, return
if(self.destinationPageIndex == self.currentPageIndex)
return;
if(self.destinationPageIndex < 0 || self.destinationPageIndex >= [self.pageViewControllerIdentifiers count])
return;
//determine the scroll direction
UIPageViewControllerNavigationDirection direction = self.destinationPageIndex < self.currentPageIndex ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward;
//get the index of the next page to scroll to
NSInteger nextPageIndex = self.destinationPageIndex < self.currentPageIndex ? self.currentPageIndex-1 : self.currentPageIndex+1;
//get the storyboard identifier of the page to navigate to
NSString *identifier = [self.pageViewControllerIdentifiers objectAtIndex:nextPageIndex];
NSString *storyboardName = @"Sales";
id viewController = [COSCheckoutNavigationViewController instantiateControllerFromStoryboardName:storyboardName storyboardIdentifier:identifier];
NSArray *viewControllers = @[viewController];
__weak typeof (self) weakSelf = self;
[self.pageViewController setViewControllers:viewControllers direction:direction animated:YES completion:^(BOOL finished)
[weakSelf updateCurrentPageIndex];
//navigate to the next page after small delay
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
[weakSelf navigateToNextPage];
);
];
有没有办法改进此代码以获得更好的滚动性能?任何其他技术可以帮助我实现我正在尝试的目标吗?
【问题讨论】:
你好@tbag你能帮帮我吗,我在pageViewController中,我想在pageviewcontroller滚动到下一页或上一页时更改我的自定义tabViews颜色变化。 【参考方案1】:Swift 3.0.2
我遇到了和你一样的问题,我找到了适合我的解决方案。请记住,我使用UISegmentedControl
管理childViewControllers
。目前,我不允许滑动手势,因为它会扰乱我的视图。此递归调用允许在 UIPageViewController
内的每个 UIViewController
之间进行动画处理。
// We call this method recursively until we find our desired view controller
// We do it this way to provide an animated transition if you find yourself in
// the first view and want to go to the last one. If we just jumped to the
// desired VC, we will not se any animation between the different VCs in the
// middle and it would look like there are only two VCs
func segmentedControl(index: Int)
let desiredVC = pages[index]
if pages[currentIndex] != desiredVC
if index > currentIndex
self.setViewControllers([pages[currentIndex+1]], direction: .forward, animated: true, completion: nil)
currentIndex += 1
else
self.setViewControllers([pages[currentIndex-1]], direction: .reverse, animated: true, completion: nil)
currentIndex -= 1
segmentedControl(index: index)
我的UISegmentedControl
只是将选定的索引发送到此方法。
【讨论】:
【参考方案2】:我不知道您的解决方案,但可能会对您有所帮助。
Pageviewcontroller
【讨论】:
以上是关于以编程方式平滑滚动 UIPageViewController的主要内容,如果未能解决你的问题,请参考以下文章