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

Posted

技术标签:

【中文标题】如何创建 Segue 的动画执行和展开 Segue?【英文标题】:How To Create Animation of Segue perform and unwind segue? 【发布时间】:2019-12-22 12:00:13 【问题描述】:

我正在开发我试图用动画调用控制器的应用程序 第二个带有动画的视图控制器 就像我用 segue 调用第二个控制器时一样 第二个控制器来自右边,第一个控制器开始向左移动 像 android push pop 动画

我写了一些代码

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)
    




但是从这段代码中,新控制器来自右侧,我也需要移动当前控制器向左移动 如果有人可以帮忙

【问题讨论】:

【参考方案1】:

试试这个代码:

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

【讨论】:

谢谢它也适用于 unwind?因为当我执行 segues 时它工作得很好 @MuhammadWaqas 我添加了放松类。希望对您有所帮助 你能指导我吗?我是 ios 的初学者,我添加了 unwind 功能,它关闭了控制器,但是当我按下后退按钮时,控制器关闭,但在 segue 上,它可以很好地使用你的代码 @MuhammadWaqas 观看 this video for unwind segue。如果我的回答对您的问题有帮助,请投票或检查它是否正确(或两者兼而有之)。谢谢兄弟! 谢谢兄弟,这与我在 ios 中想要的完全一样,你很棒:D

以上是关于如何创建 Segue 的动画执行和展开 Segue?的主要内容,如果未能解决你的问题,请参考以下文章

Segue 动画无法完全正常工作

使用后退按钮展开 segue 时执行代码

从左到右使用动画进行 Segue

如何在执行segue之前执行按钮动画[重复]

如何实现自定义segue动画?

iOS - 如何在 Unwind Segue 中控制动画类型?