【问题标题】:Set up custom segue way in Swift (top to button direction)在 Swift 中设置自定义 segue 方式(从顶部到按钮方向)
【发布时间】:2016-10-20 08:08:19
【问题描述】:

我想在故事板中的两个场景之间建立一个转场,其行为与“Apple Keynote”效果移动(从上到下方向)完全相同。现在我知道有一种叫做模态segue 的segue,但它在自下而上的方向上起作用。

请帮帮我!

我制作了一个我想要什么样的segue的视频,请观看! https://www.dropbox.com/s/zqyxrm638ellfbx/whatiwant.mov?dl=0

【问题讨论】:

    标签: ios xcode swift storyboard segue


    【解决方案1】:

    您可以像这样实现自定义UIStoryboardSegue

    class TopDownSegue: UIStoryboardSegue {
        let duration: NSTimeInterval = 1
        let delay: NSTimeInterval = 0
        let animationOptions: UIViewAnimationOptions = [.CurveEaseInOut]
    
        override func perform() {
            // get views
            let sourceView = sourceViewController.view
            let destinationView = destinationViewController.view
    
            // get screen height
            let screenHeight = UIScreen.mainScreen().bounds.size.height
            destinationView.transform = CGAffineTransformMakeTranslation(0, -screenHeight)
    
            // add destination view to view hierarchy
            UIApplication.sharedApplication().keyWindow?.insertSubview(destinationView, aboveSubview: sourceView)
    
            // animate
            UIView.animateWithDuration(duration, delay: delay, options: animationOptions, animations: { 
                destinationView.transform = CGAffineTransformIdentity
                }) { (_) in
                    self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
            }
        }
    }
    

    并在情节提要中使用新的自定义转场:

    【讨论】:

    • 非常感谢!你解决了我的问题!你能帮我解决另一个困扰我的问题吗? stackoverflow.com/questions/37900927/…
    • 很好的例子。当我使用它时,我失去了我的导航控制器“
    • 谢谢你,你的例子帮助我了解了很多关于自定义 segues 的知识。
    【解决方案2】:

    由于接受的代码已经过时,这里更新版本:

    class SegueFromTop: 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: 0, y: -src.view.frame.size.height)
    
            UIView.animate(withDuration: 0.25,
                           delay: 0.0,
                           options: .curveEaseInOut,
                           animations: {
                            dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
            },
                           completion: { finished in
                            src.present(dst, animated: false, completion: nil)
            }
            )
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多