【问题标题】:Problem with animation when using UIView.transition & UIView.AnimationOptions when transitioning between ViewControllers在 ViewController 之间转换时使用 UIView.transition 和 UIView.AnimationOptions 时出现动画问题
【发布时间】:2020-02-09 22:33:35
【问题描述】:

我正在使用 Swift 并尝试使用以下代码在菜单屏幕视图控制器 (LaunchViewController) 和游戏视图控制器 (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

        })

如何根据需要制作此过渡动画?非常感谢提前

【问题讨论】:

    标签: swift xcode uiview uiviewcontroller uiviewanimationtransition


    【解决方案1】:
    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

    【讨论】:

    • 非常感谢!这是一种享受。非常感谢。
    • @ecjps82 很高兴,然后选择这个作为答案。
    猜你喜欢
    • 2021-10-29
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多