NavigationTransitionController
关于
NavigationTransitionController
是一个 UINavigationController
,允许您实现交互式视图控制器转换。它为 4 种独特的转换类型管理 UIViewControllerInteractiveTransitioning
、UIViewControllerAnimatedTransitioning
和 UIViewControllerTransitioningDelegate
协议。
当前的 UIKit
状态要求 UIViewController
遵循多个协议来实现简单的视图控制器转换。不仅如此,它还会在将视图控制器推入或从导航栈中弹出时保留标准的 UINavigationBar
转换。
NavigationTransitionController
通过将所有设置包装在一个 UINavigationController
中来解决此实现的复杂性,这样每个 UIViewController
都可以使用自己的持续存在的 UINavigationBar
与交互式、动画转换进行交互。
使用方法
模拟将视图控制器“推”到标准导航控制器上
override func viewDidLoad() {
super.viewDidLoad()
// Initialize your view controller
let myVC = UIViewController()
// Pass it as a root view controller with the specified NavigationTransitionType
let navigationTransitionController = NavigationTransitionController(rootViewController: myVC, type: .standard)
navigationTransitionController.presentNavigation(self)
}
使用过渡类型进行演示
NavigationTransitionController
默认提供 4 种独特的自定义交互动画过渡方式,可通过 NavigationTransitionType
枚举访问。
NavigationTransitionType.Standard
NavigationTransitionType.Presentation
这模拟从底部向上“呈现”视图控制器。它限制根视图控制器的第一个滚动视图子视图,而不干扰其预期的滚动行为(即将过渡仅在 UIScrollView
的 contentOffset
达到顶部时发生)。
NavigationTransitionType.Fade
NavigationTransitionType.Zoom
这模拟了 iOS 照片库的过渡。与其他依赖项不同,其设置和拆卸非常简单。
要配置初始视图过渡
// Define your image view for the initial view transition
let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
override func viewDidLoad() {
super.viewDidLoad()
// Setup
view.addSubview(imageView)
imageView.center = view.center
// Initialize your view controller
let photoVC = UIViewController()
// MARK: - NavigationTransitionController
// Pass it as a root view controller with the specified NavigationTransitionType — here, we specify the initial view as the imageView
let navigationTransitionController = NavigationTransitionController(rootViewController: photoVC, type: .zoom, initialView: imageView)
navigationTransitionController.presentNavigation(self)
}
如果视图有任何更改(即,您正在显示水平滚动的 UICollectionView
中的多张照片),请更新 NavigationTransitionController
的 finalView
以持久化视图控制器之间的过渡图像。
// Get the cell with the new image after we've scrolled and update the final view for the navigation transition controller's transition
if let cell = collectionView.cellForItem(at: indexPath) as? ImageCell {
self.navigationTransitionController?.updateFinalView(cell.imageView)
}
使用动画关闭
您可以在使用它作为标准 UINavigationController
的 UIViewController
中访问 NavigationTransitionController
@objc func dismissViewController(_ sender: Any) {
// Dismiss the navigation transition view controller by accessing it (it's a UINavigationController subclass so you can also use ```.navigationController``` and cast it) — also call a completion if needed
self.navigationTransitionController?.dismissNavigation(animated: true, completion: { (dismissed: Bool) in
// Closure executed here
})
}
观察手势驱动的转换
有些情况下可能需要观察 NavigationTransitionController
的手势驱动转换。例如,当想要禁用 NavigationTransitionType.presentation
过渡时的滚动手势时
// Enable or disable the scroll view based on the UIGestureRecognizer.State
self.navigationTransitionController?.interactiveTransitioningObserver = { (state: UIGestureRecognizer.State) in
let isScrollEnabled = (state == .began || state == .changed) ? false : true
self.scrollView.isScrollEnabled = isScrollEnabled
}
禁用手势
要完全禁用手势
// Disable the gesture
self.navigationTransitionController?.panGestureRecognizer.isEnabled = false
要求
- iOS 13+
安装
要运行示例项目,首先克隆仓库,然后在 Example 目录下运行 pod install
NavigationTransitionController 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
pod 'NavigationTransitionController'
作者
HackShitUp,[email protected]
纳诺透镜公司,Inc.
授权
NavigationTransitionController根据MIT授权可用。更多信息请参阅LICENSE文件。