在swift中,如何使用.crossDissolve 动画执行segue?

In swift, how to perform a segue with .crossDissolve animation?

在我的第一个视图控制器中,我尝试在动画之后使用 alpha 方法执行转场。 我在 segue 中指定,modalTransitionStyle 是 .crossDissolve 但他没有这样做。他表现得像推法。

两个 viewControllers 在 navigationController 下并且在同一个故事板中,但是在两个不同的视图中。

这是我的代码:

import UIKit

class AnimationPageDeDemarrageController: UIViewController {
    
    var personne = PersonneController()
    
    @IBOutlet var slogan: UILabel!
    @IBOutlet var vueDuSlogan: UIView!
    @IBOutlet var vueBlancheDuSlogan: UIView!
    @IBOutlet var vueBlanche: UIView!
    @IBOutlet var logo: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.vueBlanche.alpha = 0
        self.slogan.alpha = 0
        self.vueDuSlogan.alpha = 0
        self.vueBlancheDuSlogan.alpha = 0
        navigationController?.setNavigationBarHidden(true, animated: false)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.faireApparaitreLaPage()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    private func faireApparaitreLaPage() {
        UIView.animate(withDuration: 1, delay: 0, animations: {
            self.vueBlanche.alpha = 1.0
            self.slogan.alpha = 1
            self.vueDuSlogan.alpha = 1
            self.vueBlancheDuSlogan.alpha = 1
        }, completion: {
            done in
            if done {
                self.segueVersMenuController()
            }
        })
    }
    
    // segue vers MenuController
    private func segueVersMenuController() {
        //Segue vers la page menuViewController
          let menuViewController = storyboard?.instantiateViewController(identifier: "MenuViewController") as? MenuViewController
        menuViewController?.modalTransitionStyle = .crossDissolve
        menuViewController?.modalPresentationStyle = .fullScreen
            menuViewController?.personne = personne

         self.navigationController?.pushViewController(menuViewController!, animated: true)
    }

}

我已经尝试了另一种类型的 segue,但应用程序崩溃了。 这是代码:

    private func presentMenuViewController() {
        let menuViewController = MenuViewController()
        menuViewController.personne = personne
        menuViewController.modalPresentationStyle = .fullScreen
        menuViewController.modalTransitionStyle = .crossDissolve
        present(menuViewController, animated: true)
    }

.modalTransitionStyle 属性 仅适用于 modal 转换。推送不是模态转换。

您需要创建 UIStoryboardSegue 的自定义子类。

我找到了我的问题的答案,这里是用来代替 segueVersMenuController() 的函数代码:

private func fadeTransition() {
        let transition: CATransition = CATransition()
        transition.duration = 1.75
        transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
        transition.type = CATransitionType.fade
        self.navigationController!.view.layer.add(transition, forKey: nil)

        let menuViewController = self.storyboard?.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
        menuViewController.personne = self.personne
        self.navigationController?.pushViewController(menuViewController, animated: false)
    }