有没有办法在没有故事板的情况下进行过渡?

Is there any way to making transition without storyboard?

我尝试制作在两个 viewController 之间切换的过渡动画。有什么办法可以不使用故事板吗?

调用新 viewController 的代码(我没有使用故事板):

 @objc func handleAccount() {
    let navController = UINavigationController(rootViewController: userButtonLauncher())
    navController.transitioningDelegate = self
    navController.modalPresentationStyle = .custom
    self.present(navController, animated: true, completion: nil)
}

此转换代码:

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.transitionMode = .present
    transition.startingPoint = CGPoint(x: 0, y: 0)
    return transition
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.transitionMode = .pop
    transition.startingPoint = CGPoint(x: 0, y: 0)
    return transition
}

当然可以!这取决于你想做什么,所以如果你需要在代码上使用默认动画而不使用故事板,你可以: 实例化您的 ViewController,设置您想要的动画并打开 ViewController

let vc = self.storyboard?.instantiateViewController(withIdentifier: "vc_id") vc.modalTransitionStyle = .flipHorizontal self.present(vc, animated: true, completion: nil)

否则您可以创建自定义转场,请参阅此简单指南: https://www.raywenderlich.com/322-custom-uiviewcontroller-transitions-getting-started

我解决了我的问题。我只是在我的家庭控制器中添加了调用动画转换的代码:

extension HomeController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return faceanim()

}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return faceanim()

}

}

按钮操作是:

    @objc func handleAccount() {

    let userConVC = UINavigationController(rootViewController: userButtonLauncher())
    userConVC.transitioningDelegate = self
    navigationController?.isNavigationBarHidden = false
    self.present(userConVC, animated: true, completion: nil)

}