首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建segue执行和解开Segue的动画?

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

Stack Overflow用户
提问于 2019-12-22 20:00:13
回答 1查看 58关注 0票数 0

我正在开发一个应用程序,我正在尝试用动画调用控制器,第二个视图控制器用动画,比如当我用segue调用第二个控制器时,第二个控制器从右边和第一个控制器开始向左移动,就像android推送弹出动画

我写了一些代码

代码语言:javascript
复制
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)
    }



}

}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-22 20:32:58

尝试以下代码:

代码语言:javascript
复制
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了解更多详细信息

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59444006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档