我正在使用从堆栈溢出的某处找到的代码从app delegate移动到新的视图控制器
func changeRootViewController(with identifier:String!) {
let storyboard = self.window?.rootViewController?.storyboard
let desiredViewController = storyboard?.instantiateViewController(withIdentifier: identifier);
let snapshot:UIView = (self.window?.snapshotView(afterScreenUpdates: true))!
desiredViewController?.view.addSubview(snapshot);
self.window?.rootViewController = desiredViewController;
UIView.animate(withDuration: 0.3, animations: {() in
snapshot.layer.opacity = 0;
snapshot.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5);
}, completion: {
(value: Bool) in
snapshot.removeFromSuperview();
});
}
它工作正常,我可以使用动画移动到新的视图控制器(需要)。但是当我转移到其他应用程序并返回时,应用程序崩溃并出错。
无法快照视图(; layer = >)with afterScreenUpdates:NO,因为 视图不在窗口中。使用afterScreenUpdates:是。致命错误: 在展开可选值时意外发现nil
我从AppDelegate打开视图控制器的其他代码
func showViewController(id: String){
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: id) as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
它完美无缺,但在尝试添加动画时,动画无法正常工作。所以请帮助解决这些问题。
答案 0 :(得分:0)
你尝试过动画吗?
UIView.transition(with: appDel.window!,
duration: 0.25,
options: .transitionCrossDissolve,
animations: { appDel.window?.rootViewController = vc } ,
completion: nil)