如何制作Segue动画来表演和放松Segue?

问题描述 投票:0回答:1

我正在尝试在其中使用动画调用控制器的应用程序带有动画的第二个视图控制器就像当我用segue呼叫第二个控制器时第二个控制器从右侧来,第一个控制器开始向左移动像android push pop animations

我输入一些代码

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)
    }



}

}

但是从此代码中,新控制器来自右侧,我也需要将当前控制器也移至左侧如果有人可以帮助

swift animation uistoryboardsegue unwind-segue
1个回答
0
投票

尝试此代码:

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以获取更多详细信息

© www.soinside.com 2019 - 2024. All rights reserved.