如何创建 Segue 执行和展开 segue 的动画?

How To Create Animation of Segue perform and unwind segue?

我正在开发应用程序,我试图用动画调用控制器 第二个带有动画的视图控制器 就像当我用 segue 调用第二个控制器时 第二个控制器来自右侧,第一个控制器开始向左移动 喜欢 android 推送流行动画

我做了一些代码

class SegueFromRight: UIStoryboardSegue {
override func perform() {
    let src = self.source
    let dst = self.destination

    src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
    dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width*2, y: 0)
    //Double the X-Axis
    UIView.animate(withDuration: 0.25, delay: 0.0, options: UIView.AnimationOptions.curveEaseInOut, animations: {
        dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
    }) { (finished) in
        src.present(dst, animated: false, completion: nil)
    }



}

}

但是根据这段代码,新控制器来自右侧,我需要将当前控制器也向左移动 如果有人可以提供帮助

试试这个代码:

class CustomSegue: UIStoryboardSegue {
    override func perform() {
        let firstVCView: UIView = self.source.view
        let secondVCView: UIView = self.destination.view
        self.destination.modalPresentationStyle = .fullScreen

        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        secondVCView.frame = CGRect(x: screenWidth, y: 0, width: screenWidth, height: screenHeight)

        let window = UIApplication.shared.keyWindow
        window?.insertSubview(secondVCView, aboveSubview: firstVCView)

        UIView.animate(withDuration: 0.4, animations: { () -> Void in
            firstVCView.frame = firstVCView.frame.offsetBy(dx: -screenWidth, dy: 0.0)
            secondVCView.frame = secondVCView.frame.offsetBy(dx: -screenWidth, dy: 0.0)
               }) { (Finished) -> Void in
                self.source.present(self.destination as UIViewController,
                       animated: false,
                       completion: nil)
           }
    }
}


class CustomSegueUnwind: UIStoryboardSegue {
    override func perform() {
        let secondVCView: UIView = self.source.view
        let firstVCView: UIView = self.destination.view
        self.destination.modalPresentationStyle = .fullScreen

        let screenWidth = UIScreen.main.bounds.size.width

        let window = UIApplication.shared.keyWindow
        window?.insertSubview(firstVCView, aboveSubview: secondVCView)

        UIView.animate(withDuration: 0.4, animations: { () -> Void in
            firstVCView.frame = firstVCView.frame.offsetBy(dx: screenWidth, dy: 0.0)
            secondVCView.frame = secondVCView.frame.offsetBy(dx: screenWidth, dy: 0.0)
               }) { (Finished) -> Void in
                self.source.dismiss(animated: false, completion: nil)
           }
    }
}

参考HERE了解更多详情