ios - 使用自定义动画呈现嵌入在导航控制器中的视图控制器
问题描述
我有 vc1,它正在呈现导航控制器,其中包含 vc2。vc1 和导航控制器之间的segue 是“Present Modally”,vc2 以标准动画出现,从下到上。
我想展示嵌入在导航控制器中的 vc2,来自 vc1 的自定义过渡动画。
我试过了
class CustomAnimator: NSObject {
func animationDuration() -> TimeInterval {
return 1.0
}
fileprivate func animateCustomTransition(using transitionContext: UIViewControllerContextTransitioning) {
// ...
}
}
extension CustomAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return animationDuration()
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
animateCustomTransition(using: transitionContext)
}
}
class CustomTransitionDelegate: NSObject {
let animator: CustomAnimator
override init() {
animator = CustomAnimator()
super.init()
}
}
extension CustomTransitionDelegate: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self.animator
}
}
class Vc2ViewController: UIViewController {
// ...
var customTranstionDelegate = CustomTransitionDelegate()
//...
}
接着:
(1) 设置vc2的transitioningDelegate。显然没有效果。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let navController = segue.destination as? UINavigationController {
if let controller = navController.viewControllers.first as? Vc2ViewController {
controller.transitioningDelegate = controller.customTranstionDelegate
}
}
}
(2) 继承 UINavigationController 并将其设置为 transitioningDelegate。vc2 以所需的方式出现,但导航栏消失了,vc2 之后的下一个视图控制器没有出现在“显示”segue 上。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? Vc2NavigationController {
controller.transitioningDelegate = controller.customTranstionDelegate
}
}
如何通过自定义过渡动画从 vc1 呈现嵌入在导航控制器中的 vc2?
解决方案
您可以使用嵌入式 - https://developer.apple.com/documentation/uikit/uiviewcontrolleranimatedtransitioning
要将用户交互添加到视图控制器转换,您必须将动画器对象与交互式动画器对象(采用 UIViewControllerInteractiveTransitioning 协议的自定义对象)一起使用。
推荐阅读
- sql - SQL server WHERE 在具有文本前缀/后缀的数字之间
- laravel - The email of the notification being sent is not working in a Laravel unit test
- ngrx - Ngrx 商店出现错误,因为类型上不存在属性“ofType”
- c# - Unity:将扫射添加到播放器控制器
- c# - How to fix System.TypeLoadException when declaring structs with union?
- java - 使用 RandomAccessFile java 读取文件中的特定索引
- android - 字体 Awsome 图标未在 Android 中显示
- business-process-management - Bonita 业务数据对象
- c# - 有没有办法在 Telerik:RadAutoCompleteBox 中多次选择一个项目?
- azure - 命名空间“Microsoft.Azure.WebJobs”中不存在 EventHubTriggerAttribute