在同一个NavigationController下的两个ViewControllers之间翻转

whi*_*625 6 uiviewcontroller uinavigationcontroller uiviewanimationtransition ios swift

我已经提到了这个问题以及关于视图/视图控制器转换的几个问题,但仍然找不到满意的答案.大多数解决方案建议翻转视图而不是视图控制器.但是,我的应用程序中的两个视图控制器具有完全不同的操作和实现逻辑,因此我避免将它们混合在一起.

在我的应用程序中,我有一个FrontViewController嵌入在NavigationController中的模态视图控制器.在视图上按下一个按钮后,模态视图控制器应该翻转BackViewController,反之亦然.我曾经尝试过以下方面FrontViewController:

let navi = UINavigationController(rootViewController: backController)
navi.modalPresentationStyle = .CurrentContext
navi.modalTransitionStyle = .FlipHorizontal
self.presentViewController(backController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

这几乎和我想要的一样,除了翻转导航栏之外.此外,如果我关闭模态视图,只有堆栈顶部的视图控制器被解除,而我无法让正确的父/呈现控制器解除模态视图中的所有其他堆栈控制器.

因此,我还试图阻止的ViewController栈并使用transitionFromViewControllerFrontViewController使用相同的导航控制器代替:

self.navigationController!.addChildViewController(backController)
self.willMoveToParentViewController(nil)

self.navigationController!.transitionFromViewController(self, toViewController: backViewController, duration: 1, options: .TransitionFlipFromLeft, animations:  {}, completion: ({Bool -> Void in
    self.removeFromParentController()
    c.didMoveToParentViewController(self)
}))
Run Code Online (Sandbox Code Playgroud)

然后我在执行时遇到了这个运行时错误: Parent view controller is using legacy containment in call to -[UIViewController transitionFromViewController:toViewController: duration:options:animations:completion:]

那么,如何在两个视图控制器之间进行转换同时防止它们保留在视图控制器堆栈中?

Joh*_*tty 16

您可以在推动视图控制器之前将自定义过渡添加到导航控制器层.

let transition = CATransition()
transition.duration = 0.3
transition.type = "flip"
transition.subtype = kCATransitionFromLeft
self.navigationController?.view.layer.addAnimation(transition, forKey: kCATransition)
self.navigationController?.pushViewController(viewController!, animated: false)
Run Code Online (Sandbox Code Playgroud)

请注意,animated参数应该是false.否则将发生默认滑动动画


Kir*_*odi 9

请参阅此演示:

flipAnimation的Swift代码:

let  mainStory = UIStoryboard(name: "Main", bundle: nil)
let search = mainStory.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
UIView.beginAnimations("animation", context: nil)
UIView.setAnimationDuration(1.0)
self.navigationController!.pushViewController(search, animated: false)
UIView.setAnimationTransition(UIViewAnimationTransition.FlipFromLeft, forView: self.navigationController!.view, cache: false)
UIView.commitAnimations()
Run Code Online (Sandbox Code Playgroud)

FlipViewController动画

输出:

在此输入图像描述


Pau*_*yer 6

Swift 5 - 我个人喜欢使用这种方法。

//PUSH
let secondVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "secondVC") as! SecondVC
self.navigationController?.pushViewController(secondVC, animated: false)
UIView.transition(from: self.view, to: secondVC.view, duration: 0.85, options: [.transitionFlipFromLeft])

//POP
let firstVC = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count ?? 2) - 2] as? FirstVC
if let firstView = firstVC?.view{
    self.navigationController?.popViewController(animated: false)
    UIView.transition(from: self.view, to: firstView, duration: 0.85, options: [.transitionFlipFromRight])
} else {
    self.navigationController?.popViewController(animated: true)
}
Run Code Online (Sandbox Code Playgroud)