1
视图控制器我使用使用此代码,我从什么地方找到的堆栈溢出移动从应用程序委托一个新的视图控制器
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();
});
}
它工作正常,我能移动到具有动画的新视图控制器(要求,要求为)。但是,只要我移动到其他应用程序并返回,该应用程序就会崩溃并出现错误。
Cannot snapshot view (; layer = >) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES. fatal error: unexpectedly found nil while unwrapping an Optional value
其他的代码,我从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()
}
它完美,但尝试添加动画时的动画效果不起作用。所以善意帮助解决这些问题。
不要误会我这里一个好的学习是“不要只是从StackOverflow复制代码” - 而是尝试理解它并由您自己实现。我之前也犯过同样的错误。 它崩溃的原因是您正在拍摄当前窗口的快照并将其用于动画。当你从后台打开应用程序'self.window'可能是零,所以你实际上不能从中获取快照。我会建议找到一种不同的方式来添加您的动画时,打开应用程序 – Bluewings
https://www.gitbook.com/book/tra38/essential-copying-and-pasting-from-stack-overflow/details –
只是检查值需要在desiredViewController存在,当你从appdelgate到达? –