【问题标题】:How To Create Animation of Segue perform and unwind segue?如何创建 Segue 的动画执行和展开 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)
    }



}

}

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

【问题讨论】:

    标签: swift animation uistoryboardsegue unwind-segue


    【解决方案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
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多