在我的 iOS 应用程序中,我需要在应用程序之间更改窗口的 rootviewController。因此,当我动态更改我的 rootviewcontroller 时,它会在更改之前轻弹 View 。但我想要的是在更改 rootviewcontroller 时提供平滑过渡.
我尝试过以下但不是我想要的过渡。
[UIView transitionWithView:self.window
duration:.8
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
self.window.rootViewController = tabBarControllerMain;
[self.window makeKeyAndVisible];
}
completion:NULL];
我想要特定的转换,例如导航 Controller pushview 转换。
任何人都可以告诉我如何实现这一目标吗?
最佳答案
根据 Hardik Darji 的回答,我创建了 UIWindow 扩展以将 rootViewController 与模拟系统动画的可配置动画类型交换 - 推送、弹出、呈现和关闭。
swift 3.
public enum SwapRootVCAnimationType {
case push
case pop
case present
case dismiss
}
extension UIWindow {
public func swapRootViewControllerWithAnimation(newViewController:UIViewController, animationType:SwapRootVCAnimationType, completion: (() -> ())? = nil)
{
guard let currentViewController = rootViewController else {
return
}
let width = currentViewController.view.frame.size.width;
let height = currentViewController.view.frame.size.height;
var newVCStartAnimationFrame: CGRect?
var currentVCEndAnimationFrame:CGRect?
var newVCAnimated = true
switch animationType
{
case .push:
newVCStartAnimationFrame = CGRect(x: width, y: 0, width: width, height: height)
currentVCEndAnimationFrame = CGRect(x: 0 - width/4, y: 0, width: width, height: height)
case .pop:
currentVCEndAnimationFrame = CGRect(x: width, y: 0, width: width, height: height)
newVCStartAnimationFrame = CGRect(x: 0 - width/4, y: 0, width: width, height: height)
newVCAnimated = false
case .present:
newVCStartAnimationFrame = CGRect(x: 0, y: height, width: width, height: height)
case .dismiss:
currentVCEndAnimationFrame = CGRect(x: 0, y: height, width: width, height: height)
newVCAnimated = false
}
newViewController.view.frame = newVCStartAnimationFrame ?? CGRect(x: 0, y: 0, width: width, height: height)
addSubview(newViewController.view)
if !newVCAnimated {
bringSubview(toFront: currentViewController.view)
}
UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations: {
if let currentVCEndAnimationFrame = currentVCEndAnimationFrame {
currentViewController.view.frame = currentVCEndAnimationFrame
}
newViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
}, completion: { finish in
self.rootViewController = newViewController
completion?()
})
makeKeyAndVisible()
}
}
关于ios - 使用 Push Transition 效果更改 RootViewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24362957/