我正在使用Swift,并尝试使用以下代码在菜单屏幕ViewController(LaunchViewController)和游戏ViewController(InGameViewController)之间切换。但是,尽管过渡正常,但无论我使用哪种动画选项(当前使用.transitionCrossDissolve
),动画始终从左上角开始出现,并扩展为填满整个屏幕。我可以更改持续时间,并按预期调整动画,但是无论我使用什么UIView.AnimationOption,它始终使用相同的动画。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "InGameViewController")
vc.view.frame = (self.view?.frame)!
vc.view.layoutIfNeeded()
UIView.transition(with: self.view!, duration: 0.3, options: .transitionCrossDissolve, animations: {
self.view?.window?.rootViewController = vc
}, completion: { (true) in
})
如何根据需要制作此过渡动画?提前非常感谢
func changeRootViewControllerTo(_ controller: UIViewController, animated: Bool = false) {
UIApplication.shared.keyWindow?.rootViewController = controller
guard
animated,
let window = UIApplication.shared.keyWindow else {
return
}
UIView.transition(with: window,
duration: 0.3,
options: .transitionCrossDissolve,
animations: nil,
completion: nil)
}
就我而言,这很完美。我认为您不需要添加这些行vc.view.frame = (self.view?.frame)!
vc.view.layoutIfNeeded()
并通过转换方法将self.view
更改为window
。