YTPageController 引入了一种整洁且通用的解决方案,以在 scroll 之间流畅地转换视图控制器,就像 Apple 在其音乐应用中那样做
您可以在您的新应用中用 YTPageController 替换原有的 UIPageViewController
,您不再需要监听 contentOffset 变化或更改 tint 颜色或框架。
YTPageController 提供了一个名为 pageCoordinator
的属性,类似于 UIViewController 中的 过渡协调器,供您在页面转换期间执行自己的动画。
例如,要动画 UISegmentedControl
,您只需在 YTPageControllerDelegate
中添加以下几行代码
// Called before a transition starts
- (void)pageController:(YTPageController *)pageController willStartTransition:(id<YTPageTransitionContext>)context {
// Add your own animations using `pageCoordinator`
[pageController.pageCoordinator animateAlongsidePagingInView:self.segmentedControl animation:^(id<YTPageTransitionContext> _Nonnull context) {
// Update your segmented control according to the context object.
self.segmentedControl.userInteractionEnabled = NO;
self.segmentedControl.selectedSegmentIndex = [context toIndex];
} completion:^(id<YTPageTransitionContext> _Nonnull context) {
if ([context isCanceled]) {
// If transition canceled, restore to the previous state
self.segmentedControl.selectedSegmentIndex = [context fromIndex];
}
self.segmentedControl.userInteractionEnabled = YES;
}];
}
YTPageController 与 UIKit 或第三方 UI 控件结合得非常好,例如 AKASegmentedControl
或任何可动画的视图
有关详细信息,请参阅示例项目。
您有两种方法可以快速设置 YTPageController
您可以使用 YTPageController
作为子视图控制器或者通过子类化它。无论哪种方式,您都必须通过设置 dataSource
属性来提供您的视图控制器
pageController.dataSource = /* your data source object */
...或 viewControllers
属性
pageController.viewControllers = /* an array of child view controllers */
如果设置了这两个属性,则 dataSource
将具有优先级。
您也可以像以前用 UITabBarController
一样,不使用任何代码就在故事板中设置 YTPageController
由于 Apple 没有提供自定义关系 segue,您需要按照以下步骤来模拟它
YTPageController
拖动一个自定义 segue 到您的子视图控制器之一并将其类更改为 YTPageControllerSegue
;YTPage_{index}
命名此 segue 的标识符,例如 YTPage_0
、YTPage_1
、YTPage_2
、...YTPageController 将在运行时找到并执行所有这些 segue,将连接的视图控制器添加到其子视图控制器中。
要运行示例项目,克隆仓库,然后首先从示例目录运行 pod install
iOS 8.0 或更高版本。可能从 iOS 6.0 开始起作用,但尚未进行测试。
YTPageController 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "YTPageController"
Yeatse CC, [email protected]
YTPageController 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。