基礎上Hardik Darji的答案,我已經創建了一個UIWindow擴展交換RootViewController的與模擬系統的動畫配置的動畫類型 - 推,流行,目前和罷免。
斯威夫特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()
}
}
這工作然而有沒有辦法爲前視圖 - 控制到不黑?一旦動畫開始,執行代碼的視圖控制器就會變黑。 –
在Swift中,檢查這個答案...它的工作 http://stackoverflow.com/a/35224877/1000906 –
這看起來不像UINavigationController中的過渡,你必須使用一個假的vc –